math/rand/v2 (Go 1.22+) replaces the old math/rand: PCG generator, no global Seed(), and rand.New(rand.NewPCG(seed, 0)) gives an isolated deterministic stream. Each randomness consumer (graph, thresholds, education sampling) will get its own stream so levers vary independently. Tests live next to the code as *_test.go; 'go test ./...' runs them all. 'for i := range 100' is Go 1.22 range-over-int.
26 lines
716 B
Go
26 lines
716 B
Go
package engine
|
|
|
|
import "testing"
|
|
|
|
// Determinism is a locked project decision: the same seed must always yield
|
|
// the same stream, and different seeds must not collide. With fixed seeds
|
|
// these assertions are exact, not probabilistic.
|
|
|
|
func TestNewRandSameSeedSameStream(t *testing.T) {
|
|
a, b := newRand(17), newRand(17)
|
|
for i := range 100 {
|
|
if got, want := a.Float64(), b.Float64(); got != want {
|
|
t.Fatalf("draw %d: streams diverged: %v != %v", i, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNewRandDifferentSeedsDiffer(t *testing.T) {
|
|
a, b := newRand(17), newRand(18)
|
|
for range 100 {
|
|
if a.Float64() != b.Float64() {
|
|
return // diverged, as expected
|
|
}
|
|
}
|
|
t.Fatal("seeds 17 and 18 produced 100 identical draws")
|
|
}
|