Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "math/rand"
  6. "time"
  7. )
  8.  
  9. func init() {
  10. rand.Seed(time.Now().UnixNano())
  11. }
  12.  
  13. const failureRate = 25
  14. const numOfExperiments = 1e3
  15.  
  16. func main() {
  17. component1 := component(failureRate)
  18. component2 := component(failureRate)
  19.  
  20. var results = make(map[bool]int)
  21. for i := 0; i < numOfExperiments; i++ {
  22. r := callAnd(component1, component2)
  23. results[r]++
  24. }
  25.  
  26. fmt.Println("Ok: ", results[true])
  27. fmt.Println("Failures: ", results[false])
  28. fmt.Printf("Failure rate %v\n", float64(results[false]) / float64(results[true] + results[false]))
  29. }
  30.  
  31. type callable func() bool
  32.  
  33. // отказ одного из компонентов приводит к отказу всего метода
  34. func callAnd(components ...callable) bool {
  35. for _, component := range components {
  36. if !component() {
  37. return false
  38. }
  39. }
  40.  
  41. return true
  42. }
  43.  
  44. func callOr(components ...callable) bool {
  45. for _, component := range components {
  46. if component() {
  47. return true
  48. }
  49. }
  50.  
  51. return false
  52. }
  53.  
  54. func component(failureRate int) callable {
  55. if failureRate < 0 || failureRate > 100 {
  56. panic("wrong failure rate")
  57. }
  58.  
  59. return func() bool {
  60. r := rand.Intn(100)
  61. return failureRate <= r
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement