v/vlib/rand/pcg32.v

70 lines
1.8 KiB
V
Raw Normal View History

module rand
// Ported from http://www.pcg-random.org/download.html
// and https://github.com/imneme/pcg-c-basic/blob/master/pcg_basic.c
2019-11-19 12:26:14 +01:00
pub struct Pcg32 {
mut:
state u64
2019-12-19 22:29:37 +01:00
inc u64
}
2019-12-19 22:29:37 +01:00
/**
* new_pcg32 - a Pcg32 PRNG generator
2019-12-07 13:51:00 +01:00
* @param initstate - the initial state of the PRNG.
* @param initseq - the stream/step of the PRNG.
* @return a new Pcg32 PRNG instance
*/
2019-12-19 22:29:37 +01:00
pub fn new_pcg32(initstate u64, initseq u64) Pcg32 {
2019-12-19 22:29:37 +01:00
mut rng := Pcg32{
}
rng.state = u64(0)
2019-12-19 22:29:37 +01:00
rng.inc = (initseq<<u64(1)) | u64(1)
rng.next()
rng.state += initstate
rng.next()
return rng
}
2019-12-19 22:29:37 +01:00
/**
* Pcg32.next - update the PRNG state and get back the next random number
* @return the generated pseudo random number
*/
2019-12-19 22:29:37 +01:00
[inline]
pub fn (rng mut Pcg32) next() u32 {
oldstate := rng.state
2019-12-07 13:51:00 +01:00
rng.state = oldstate * (6364136223846793005) + rng.inc
2019-12-19 22:29:37 +01:00
xorshifted := u32(((oldstate>>u64(18)) ^ oldstate)>>u64(27))
rot := u32(oldstate>>u64(59))
return ((xorshifted>>rot) | (xorshifted<<((-rot) & u32(31))))
}
2019-12-19 22:29:37 +01:00
/**
* Pcg32.bounded_next - update the PRNG state. Get the next number < bound
* @param bound - the returned random number will be < bound
* @return the generated pseudo random number
*/
2019-12-19 22:29:37 +01:00
[inline]
pub fn (rng mut Pcg32) bounded_next(bound u32) u32 {
// To avoid bias, we need to make the range of the RNG a multiple of
// bound, which we do by dropping output less than a threshold.
2019-12-19 22:29:37 +01:00
threshold := (-bound % bound)
// Uniformity guarantees that loop below will terminate. In practice, it
// should usually terminate quickly; on average (assuming all bounds are
// equally likely), 82.25% of the time, we can expect it to require just
// one iteration. In practice, bounds are typically small and only a
// tiny amount of the range is eliminated.
for {
r := rng.next()
if r >= threshold {
2019-12-19 22:29:37 +01:00
return (r % bound)
2019-12-07 13:51:00 +01:00
}
}
return u32(0)
}
2019-12-19 22:29:37 +01:00