v / vlib / rand / wyrand / z_wyrand.c.v
21 lines · 18 sloc · 480 bytes · 55e8faaedf64ec9e77581af873479891baf249d1
Raw
1module wyrand
2
3fn C._wymix(u64, u64) u64
4
5// free should be called when the generator is no longer needed.
6// WyRandRNG contains no heap-allocated fields, so this is a no-op.
7@[unsafe]
8pub fn (mut rng WyRandRNG) free() {
9}
10
11// u64 returns a pseudorandom 64bit int in range `[0, 2⁶⁴)`.
12@[ignore_overflow; inline]
13pub fn (mut rng WyRandRNG) u64() u64 {
14 unsafe {
15 mut seed1 := rng.state
16 seed1 += wyp0
17 rng.state = seed1
18 return C._wymix(seed1 ^ wyp1, seed1)
19 }
20 return 0
21}
22