comparison pkg/common/random.go @ 3650:01ce3ba9b0d0 single-beam

Fixed generating of random points.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 13 Jun 2019 13:14:40 +0200
parents 810b28f59b8b
children 8c5df0f3562e
comparison
equal deleted inserted replaced
3646:810b28f59b8b 3650:01ce3ba9b0d0
16 import ( 16 import (
17 "bytes" 17 "bytes"
18 "crypto/rand" 18 "crypto/rand"
19 "io" 19 "io"
20 "log" 20 "log"
21 "math"
21 "math/big" 22 "math/big"
22 mrand "math/rand" 23 mrand "math/rand"
23 "time" 24 "time"
24 ) 25 )
25 26
69 70
70 func Random(low, high float64) func() float64 { 71 func Random(low, high float64) func() float64 {
71 if low > high { 72 if low > high {
72 low, high = high, low 73 low, high = high, low
73 } 74 }
74 rnd := mrand.New(mrand.NewSource(time.Now().Unix())) 75
76 var seed int64
77 if seedInt, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64)); err != nil {
78 log.Printf("warn: Generating good random seed failed: %v\n", err)
79 seed = time.Now().Unix()
80 } else {
81 seed = seedInt.Int64()
82 }
83 rnd := mrand.New(mrand.NewSource(seed))
75 m := high - low 84 m := high - low
76 return func() float64 { return rnd.Float64()*m + low } 85 return func() float64 { return rnd.Float64()*m + low }
77 } 86 }