Answer architecture round 1: read the module path from go.mod, keep the randomness behind State's methods, rename the field drawState and the counter Seq, and file items 108 and 109
This commit is contained in:
@@ -1,22 +1,26 @@
|
||||
// Package drawstate holds what a generator draws through: the seeded randomness and the
|
||||
// {seq()} counters.
|
||||
// Package drawstate holds a generator's seeded randomness, which its draws and builtin
|
||||
// samples read, and its {seq()} counters.
|
||||
package drawstate
|
||||
|
||||
import "math/rand/v2"
|
||||
|
||||
// State is one generator's draw state, kept across its renders.
|
||||
type State struct {
|
||||
*rand.Rand
|
||||
rand *rand.Rand
|
||||
counters map[string]uint64
|
||||
}
|
||||
|
||||
// New builds the state a seed reproduces.
|
||||
func New(seed uint64) *State {
|
||||
return &State{Rand: rand.New(rand.NewPCG(seed, seed^0x9e3779b97f4a7c15)), counters: map[string]uint64{}}
|
||||
return &State{rand: rand.New(rand.NewPCG(seed, seed^0x9e3779b97f4a7c15)), counters: map[string]uint64{}}
|
||||
}
|
||||
|
||||
// Next advances the counter key names, the one {seq()} or {seq(key)} reads, and returns it.
|
||||
func (s *State) Next(key string) uint64 {
|
||||
func (s *State) IntN(n int) int { return s.rand.IntN(n) }
|
||||
|
||||
func (s *State) Float64() float64 { return s.rand.Float64() }
|
||||
|
||||
// Seq advances the counter {seq(key)} reads, or {seq()} for an empty key, and returns it.
|
||||
func (s *State) Seq(key string) uint64 {
|
||||
s.counters[key]++
|
||||
return s.counters[key]
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import "testing"
|
||||
func TestOneSeedDrawsOneSequence(t *testing.T) {
|
||||
a, b := New(42), New(42)
|
||||
for i := 0; i < 50; i++ {
|
||||
if x, y := a.Uint64(), b.Uint64(); x != y {
|
||||
if x, y := a.IntN(1<<30), b.IntN(1<<30); x != y {
|
||||
t.Fatalf("seed 42 diverged at draw %d: %d != %d", i, x, y)
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ func TestOneSeedDrawsOneSequence(t *testing.T) {
|
||||
func TestTwoSeedsDrawTwoSequences(t *testing.T) {
|
||||
a, b := New(1), New(2)
|
||||
for i := 0; i < 50; i++ {
|
||||
if a.Uint64() != b.Uint64() {
|
||||
if a.IntN(1<<30) != b.IntN(1<<30) {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -24,17 +24,17 @@ func TestTwoSeedsDrawTwoSequences(t *testing.T) {
|
||||
func TestCountersCountFromOneEach(t *testing.T) {
|
||||
s := New(1)
|
||||
for want := uint64(1); want <= 3; want++ {
|
||||
if got := s.Next(""); got != want {
|
||||
t.Fatalf("Next(\"\") = %d, want %d", got, want)
|
||||
if got := s.Seq(""); got != want {
|
||||
t.Fatalf("Seq(\"\") = %d, want %d", got, want)
|
||||
}
|
||||
}
|
||||
if got := s.Next("orders"); got != 1 {
|
||||
t.Fatalf("Next(orders) = %d, want its own count from 1", got)
|
||||
if got := s.Seq("orders"); got != 1 {
|
||||
t.Fatalf("Seq(orders) = %d, want its own count from 1", got)
|
||||
}
|
||||
if got := s.Next(""); got != 4 {
|
||||
t.Fatalf("Next(\"\") after Next(orders) = %d, want 4", got)
|
||||
if got := s.Seq(""); got != 4 {
|
||||
t.Fatalf("Seq(\"\") after Seq(orders) = %d, want 4", got)
|
||||
}
|
||||
if got := New(1).Next(""); got != 1 {
|
||||
t.Fatalf("a new state's Next = %d, want 1", got)
|
||||
if got := New(1).Seq(""); got != 1 {
|
||||
t.Fatalf("a new state's Seq = %d, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user