-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcg.go
49 lines (42 loc) · 906 Bytes
/
lcg.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
package pnc
// http://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use
// Numerical Recipes 232 1664525 1013904223
const (
lcg_modulus = 1<<32
lcg_multiplier = 1664525
lcg_increment = 1013904223
)
type LCG struct {
State uint32
StateBit uint32
StateIndex uint64
}
func NewLCG() LCG {
l := LCG{}
return l
}
func (l *LCG) Seed(seed uint32) {
l.State = seed
l.generate_number()
}
func (l *LCG) generate_number() {
l.StateIndex++
l.StateBit = 0
l.State = uint32((lcg_multiplier * uint64(l.State) + lcg_increment) % lcg_modulus)
}
func (l *LCG) Bit() uint32 {
bit_index_from_left := (31 - l.StateBit)
bit := 1 & (l.State>>bit_index_from_left)
l.StateBit++
if l.StateBit == 32 {
l.generate_number()
}
return bit
}
func (l *LCG) Urand32() uint32 {
v := uint32(0)
for i := 0; i < 32; i++ {
v = (v<<1) + l.Bit()
}
return v
}