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,48 +1,58 @@
module rand
// Ported from http://www.pcg-random.org/download.html
// and https://github.com/imneme/pcg-c-basic/blob/master/pcg_basic.c
pub struct Pcg32 {
mut:
state u64
inc u64
inc u64
}
/**
* new_pcg32 - a Pcg32 PRNG generator
* @param initstate - the initial state of the PRNG.
* @param initseq - the stream/step of the PRNG.
* @return a new Pcg32 PRNG instance
*/
pub fn new_pcg32(initstate u64, initseq u64) Pcg32 {
mut rng := Pcg32{}
mut rng := Pcg32{
}
rng.state = u64(0)
rng.inc = (initseq << u64(1)) | u64(1)
rng.inc = (initseq<<u64(1)) | u64(1)
rng.next()
rng.state += initstate
rng.next()
return rng
}
/**
* Pcg32.next - update the PRNG state and get back the next random number
* @return the generated pseudo random number
*/
[inline] pub fn (rng mut Pcg32) next() u32 {
[inline]
pub fn (rng mut Pcg32) next() u32 {
oldstate := rng.state
rng.state = oldstate * (6364136223846793005) + rng.inc
xorshifted := u32( ( (oldstate >> u64(18)) ^ oldstate) >> u64(27) )
rot := u32( oldstate >> u64(59) )
return ( (xorshifted >> rot) | (xorshifted << ((-rot) & u32(31))) )
xorshifted := u32(((oldstate>>u64(18)) ^ oldstate)>>u64(27))
rot := u32(oldstate>>u64(59))
return ((xorshifted>>rot) | (xorshifted<<((-rot) & u32(31))))
}
/**
* 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
*/
[inline] pub fn (rng mut Pcg32) bounded_next(bound u32) u32 {
[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.
threshold := ( -bound % bound )
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
@@ -51,8 +61,9 @@ pub fn new_pcg32(initstate u64, initseq u64) Pcg32 {
for {
r := rng.next()
if r >= threshold {
return ( r % bound )
return (r % bound)
}
}
return u32(0)
}

View File

@@ -1,7 +1,6 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module rand
pub fn seed(s int) {
@@ -13,7 +12,6 @@ pub fn next(max int) int {
}
fn C.rand() int
/**
* rand_r - reentrant pseudo-random number generator
*
@@ -21,11 +19,15 @@ fn C.rand() int
*
* @return a value between 0 and C.RAND_MAX (inclusive)
*/
pub fn rand_r(seed &int) int {
unsafe {
mut rs := seed
ns := ( *rs * 1103515245 + 12345 )
*rs = ns
return ns & 0x7fffffff
unsafe{
mut rs := seed
ns := (*rs * 1103515245 + 12345)
*rs = ns
return ns & 0x7fffffff
}
}

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)
}