initial import
This commit is contained in:
26
core/mathx/proba.go
Normal file
26
core/mathx/proba.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package mathx
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Proba struct {
|
||||
// rand.New(...) returns a non thread safe object
|
||||
r *rand.Rand
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func NewProba() *Proba {
|
||||
return &Proba{
|
||||
r: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Proba) TrueOnProba(proba float64) (truth bool) {
|
||||
p.lock.Lock()
|
||||
truth = p.r.Float64() < proba
|
||||
p.lock.Unlock()
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user