mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
rand: separate rand.util and rand.seed submodules (#8353)
This commit is contained in:
@@ -4,8 +4,9 @@
|
||||
module wyrand
|
||||
|
||||
import math.bits
|
||||
import rand.seed
|
||||
import rand.util
|
||||
import hash as wyhash
|
||||
import hash
|
||||
|
||||
// Redefinition of some constants that we will need for pseudorandom number generation.
|
||||
const (
|
||||
@@ -16,7 +17,7 @@ const (
|
||||
// WyRandRNG is a RNG based on the WyHash hashing algorithm.
|
||||
pub struct WyRandRNG {
|
||||
mut:
|
||||
state u64 = util.time_seed_64()
|
||||
state u64 = seed.time_seed_64()
|
||||
has_extra bool
|
||||
extra u32
|
||||
}
|
||||
@@ -51,9 +52,9 @@ pub fn (mut rng WyRandRNG) u32() u32 {
|
||||
pub fn (mut rng WyRandRNG) u64() u64 {
|
||||
unsafe {
|
||||
mut seed1 := rng.state
|
||||
seed1 += wyp0
|
||||
seed1 += wyrand.wyp0
|
||||
rng.state = seed1
|
||||
return wyhash.wymum(seed1 ^ wyp1, seed1)
|
||||
return hash.wymum(seed1 ^ wyrand.wyp1, seed1)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
import math
|
||||
import rand.util
|
||||
import rand.seed
|
||||
import rand.wyrand
|
||||
|
||||
const (
|
||||
@@ -16,7 +16,7 @@ const (
|
||||
|
||||
fn gen_randoms(seed_data []u32, bound int) []u64 {
|
||||
bound_u64 := u64(bound)
|
||||
mut randoms := []u64{len:(20)}
|
||||
mut randoms := []u64{len: (20)}
|
||||
mut rnd := wyrand.WyRandRNG{}
|
||||
rnd.seed(seed_data)
|
||||
for i in 0 .. 20 {
|
||||
@@ -26,7 +26,7 @@ fn gen_randoms(seed_data []u32, bound int) []u64 {
|
||||
}
|
||||
|
||||
fn test_wyrand_reproducibility() {
|
||||
seed_data := util.time_seed_array(2)
|
||||
seed_data := seed.time_seed_array(2)
|
||||
randoms1 := gen_randoms(seed_data, 1000)
|
||||
randoms2 := gen_randoms(seed_data, 1000)
|
||||
assert randoms1.len == randoms2.len
|
||||
|
Reference in New Issue
Block a user