Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "math/rand"
- "time"
- )
- func init() {
- rand.Seed(time.Now().UnixNano())
- }
- const failureRate = 25
- const numOfExperiments = 1e3
- func main() {
- component1 := component(failureRate)
- component2 := component(failureRate)
- var results = make(map[bool]int)
- for i := 0; i < numOfExperiments; i++ {
- r := callAnd(component1, component2)
- results[r]++
- }
- fmt.Println("Ok: ", results[true])
- fmt.Println("Failures: ", results[false])
- fmt.Printf("Failure rate %v\n", float64(results[false]) / float64(results[true] + results[false]))
- }
- type callable func() bool
- // отказ одного из компонентов приводит к отказу всего метода
- func callAnd(components ...callable) bool {
- for _, component := range components {
- if !component() {
- return false
- }
- }
- return true
- }
- func callOr(components ...callable) bool {
- for _, component := range components {
- if component() {
- return true
- }
- }
- return false
- }
- func component(failureRate int) callable {
- if failureRate < 0 || failureRate > 100 {
- panic("wrong failure rate")
- }
- return func() bool {
- r := rand.Intn(100)
- return failureRate <= r
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement