-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample_test.go
256 lines (194 loc) · 5.46 KB
/
Example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2023 University of Illinois Board of Trustees.
// All rights reserved.
package rngstream_test
import (
"fmt"
"github.com/iti/rngstream"
)
var initialSeed = []uint64{12345, 12345, 12345, 12345, 12345, 12345}
func ExampleNew() {
// Reset seed to make test deterministic
rngstream.SetPackageSeed(initialSeed)
// Create a random number generator
g := rngstream.New("g")
// Fetch some pseudo-random numbers
f1 := g.RandU01()
f2 := g.RandU01()
fmt.Printf("%.5f %.5f\n", f1, f2)
// Output: 0.12701 0.31853
}
func ExampleRngStream_RandU01() {
// Reset seed to make test deterministic
rngstream.SetPackageSeed(initialSeed)
// Create a random number generator
g := rngstream.New("g")
// Fetch some pseudo-random numbers
f1 := g.RandU01()
f2 := g.RandU01()
fmt.Printf("%.5f %.5f\n", f1, f2)
// Output: 0.12701 0.31853
}
func ExampleNew_double() {
// Reset seed to make test deterministic
rngstream.SetPackageSeed(initialSeed)
// Each call to New gets a distinct seed
g1 := rngstream.New("g1")
g2 := rngstream.New("g2")
// Fetch some pseudo-random numbers
f1_1 := g1.RandU01()
f1_2 := g1.RandU01()
f2_1 := g2.RandU01()
f2_2 := g2.RandU01()
fmt.Printf("%.5f %.5f %.5f %.5f\n", f1_1, f1_2, f2_1, f2_2)
// Output: 0.12701 0.31853 0.75958 0.97831
}
func ExampleSetPackageSeed_equal() {
// Construct two generators with identical seeds
rngstream.SetPackageSeed(initialSeed)
g1 := rngstream.New("g1")
rngstream.SetPackageSeed(initialSeed)
g2 := rngstream.New("g2")
b1 := g1.RandU01() == g2.RandU01()
b2 := g1.RandU01() == g2.RandU01()
fmt.Printf("%v %v\n", b1, b2)
// Output: true true
}
func ExampleSetPackageSeed_notequal() {
// Construct two generators, but allow the seed to advance for second
rngstream.SetPackageSeed(initialSeed)
g1 := rngstream.New("g1")
g2 := rngstream.New("g2")
b1 := g1.RandU01() == g2.RandU01()
b2 := g1.RandU01() == g2.RandU01()
fmt.Printf("%v %v\n", b1, b2)
// Output: false false
}
func ExampleSetRngStreamMasterSeed_equal() {
// Construct two generators with identical seeds
rngstream.SetRngStreamMasterSeed(5555)
g1 := rngstream.New("g1")
rngstream.SetRngStreamMasterSeed(5555)
g2 := rngstream.New("g2")
b1 := g1.RandU01() == g2.RandU01()
b2 := g1.RandU01() == g2.RandU01()
fmt.Printf("%v %v\n", b1, b2)
// Output: true true
}
func ExampleSetRngStreamMasterSeed_notequal() {
// Construct two generators, but allow the seed to advance for second
rngstream.SetRngStreamMasterSeed(5555)
g1 := rngstream.New("g1")
g2 := rngstream.New("g2")
b1 := g1.RandU01() == g2.RandU01()
b2 := g1.RandU01() == g2.RandU01()
fmt.Printf("%v %v\n", b1, b2)
// Output: false false
}
func ExampleRngStream() {
var g *rngstream.RngStream
// Reset seed to make test deterministic
rngstream.SetPackageSeed(initialSeed)
g = rngstream.New("g")
b := g.RandU01() == g.RandU01()
fmt.Printf("%v\n", b)
// Output: false
}
func ExampleRngStream_AdvanceState_equal() {
// Construct two generators with identical seeds
rngstream.SetRngStreamMasterSeed(5555)
g1 := rngstream.New("g1")
rngstream.SetRngStreamMasterSeed(5555)
g2 := rngstream.New("g2")
// Consume 10 random numbers
for i := 0; i < 10; i++ {
g1.RandU01()
}
// Consume 10 random numbers
g2.AdvanceState(0, 10)
b := g1.RandU01() == g2.RandU01()
fmt.Printf("%v\n", b)
// Output: true
}
func ExampleRngStream_GetState() {
// Construct generator with known state
rngstream.SetRngStreamMasterSeed(5555)
g1 := rngstream.New("g1")
// Consume 10 random numbers
g1.AdvanceState(0, 10)
// Grab the current state & get a random number.
state := g1.GetState()
r1 := g1.RandU01()
// Consume 10 more random numbers
g1.AdvanceState(0, 10)
// Restore the state & get a random number.
g1.SetSeed(state)
r2 := g1.RandU01()
b := r1 == r2
fmt.Printf("%v\n", b)
// Output: true
}
func ExampleRngStream_SetSeed() {
// Construct generator with known state
rngstream.SetRngStreamMasterSeed(5555)
g1 := rngstream.New("g1")
// Consume 10 random numbers
g1.AdvanceState(0, 10)
// Grab the current state & get a random number.
state := g1.GetState()
r1 := g1.RandU01()
// Consume 10 more random numbers
g1.AdvanceState(0, 10)
// Restore the state & get a random number.
g1.SetSeed(state)
r2 := g1.RandU01()
b := r1 == r2
fmt.Printf("%v\n", b)
// Output: true
}
func ExampleRngStream_RandInt() {
// Construct generator with known state
rngstream.SetRngStreamMasterSeed(5555)
g1 := rngstream.New("g1")
// Generate some numbers
fmt.Printf("%v %v %v %v\n",
g1.RandInt(1, 3),
g1.RandInt(1, 3),
g1.RandInt(1, 3),
g1.RandInt(1, 3))
// Output: 3 3 1 2
}
func ExampleRngStream_WriteState() {
// Reset seed to make test deterministic
rngstream.SetPackageSeed(initialSeed)
// Construct a generator
g := rngstream.New("g")
// Initial state value
g.WriteState()
// Consume 10 random numbers
for i := 0; i < 10; i++ {
g.RandU01()
}
// Current state value
g.WriteState()
// Output:
// g:
// Cg = {12345,12345,12345,12345,12345,12345 }
//
// g:
// Cg = {2989318136,3378525425,1773647758,1462200156,2794459678,2822254363 }
}
func ExampleRngStream_WriteStateFull() {
// Reset seed to make test deterministic
rngstream.SetPackageSeed(initialSeed)
// Construct a generator
g := rngstream.New("g")
// Initial state value
g.WriteStateFull()
// Output:
// g:
// Anti = false
// IncPrec = false
// Ig = { 12345,12345,12345,12345,12345,12345 }
// Bg = { 12345,12345,12345,12345,12345,12345 }
// Cg = { 12345,12345,12345,12345,12345,12345}
}