mirror of
https://github.com/johnkerl/miller.git
synced 2026-07-22 07:30:43 +00:00
Miller assumes 64-bit integers, but in Go, the int type varies in size depending on the architecture: 32-bit architectures have int equivalent to int32. As a result, the supported range of integer values is greatly reduced on 32-bit architectures compared to what is suggested by the documentation. This patch explicitly uses int64 wherever 64-bit integers are assumed. Test cases affected by the behaviour of the random generator are updated to reflect the new values (the existing seed doesn't produce the same behaviour since the way random values are generated has changed). Signed-off-by: Stephen Kitt <steve@sk2.org>
42 lines
1 KiB
Go
42 lines
1 KiB
Go
// ================================================================
|
|
// Thinly wraps Go's rand library, with seed-function support
|
|
// ================================================================
|
|
|
|
package lib
|
|
|
|
import (
|
|
"math/rand"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
// By default, Miller random numbers are different on every run.
|
|
var defaultSeed = time.Now().UnixNano() ^ int64(os.Getpid())
|
|
var source = rand.NewSource(defaultSeed)
|
|
var generator = rand.New(source)
|
|
|
|
// Users can request specific seeds if they want the same random-number
|
|
// sequence on each run.
|
|
func SeedRandom(seed int64) {
|
|
source = rand.NewSource(seed)
|
|
generator = rand.New(source)
|
|
}
|
|
|
|
func RandFloat64() float64 {
|
|
return generator.Float64()
|
|
}
|
|
func RandUint32() uint32 {
|
|
return generator.Uint32()
|
|
}
|
|
func RandInt63() int64 {
|
|
return generator.Int63()
|
|
}
|
|
func RandRange(lowInclusive, highExclusive int64) int64 {
|
|
if lowInclusive == highExclusive {
|
|
return lowInclusive
|
|
} else {
|
|
u := generator.Int63()
|
|
// TODO: test divide-by-zero cases in UT
|
|
return lowInclusive + (u % (highExclusive - lowInclusive))
|
|
}
|
|
}
|