1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
Alexander Medvednikov
2019-12-20 00:29:37 +03:00
parent b6fe2ebc0b
commit 6210984c97
54 changed files with 1757 additions and 1993 deletions

View File

@ -1,36 +1,46 @@
module rand
// Ported from http://xoshiro.di.unimi.it/splitmix64.c
struct Splitmix64 {
mut:
state u64
}
/**
* new_splitmix64 - a Splitmix64 PRNG generator
* @param seed the initial seed of the PRNG.
* @return a new Splitmix64 PRNG instance
*/
pub fn new_splitmix64(seed u64) Splitmix64 {
return Splitmix64{ seed }
return Splitmix64{
seed}
}
/**
* Splitmix64.next - update the PRNG state and get back the next random number
* @return the generated pseudo random number
*/
[inline] pub fn (rng mut Splitmix64) next() u64 {
[inline]
pub fn (rng mut Splitmix64) next() u64 {
rng.state += (0x9e3779b97f4a7c15)
mut z := rng.state
z = (z ^ ((z >> u64(30)))) * (0xbf58476d1ce4e5b9)
z = (z ^ ((z >> u64(27)))) * (0x94d049bb133111eb)
return z ^ (z >> (31))
z = (z ^ ((z>>u64(30)))) * (0xbf58476d1ce4e5b9)
z = (z ^ ((z>>u64(27)))) * (0x94d049bb133111eb)
return z ^ (z>>(31))
}
/**
* Splitmix64.bounded_next - Get the next random number < bound
* @param bound - the returned random number will be < bound
* @return the generated pseudo random number
*/
[inline] pub fn (rng mut Splitmix64) bounded_next(bound u64) u64 {
[inline]
pub fn (rng mut Splitmix64) bounded_next(bound u64) u64 {
threshold := -bound % bound
for {
r := rng.next()
@ -40,3 +50,4 @@ pub fn new_splitmix64(seed u64) Splitmix64 {
}
return u64(0)
}