proba.go 396 B

1234567891011121314151617181920212223242526
  1. package mathx
  2. import (
  3. "math/rand"
  4. "sync"
  5. "time"
  6. )
  7. type Proba struct {
  8. // rand.New(...) returns a non thread safe object
  9. r *rand.Rand
  10. lock sync.Mutex
  11. }
  12. func NewProba() *Proba {
  13. return &Proba{
  14. r: rand.New(rand.NewSource(time.Now().UnixNano())),
  15. }
  16. }
  17. func (p *Proba) TrueOnProba(proba float64) (truth bool) {
  18. p.lock.Lock()
  19. truth = p.r.Float64() < proba
  20. p.lock.Unlock()
  21. return
  22. }