From f5c4671bfbad96bf346bd7e9a21fc4317b4959df Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Sat, 3 Dec 2022 17:00:20 +0530 Subject: Adds most of the tools --- v_windows/v/vlib/rand/wyrand/wyrand.v | 252 ++++++++++++++++++++++ v_windows/v/vlib/rand/wyrand/wyrand_test.v | 331 +++++++++++++++++++++++++++++ 2 files changed, 583 insertions(+) create mode 100644 v_windows/v/vlib/rand/wyrand/wyrand.v create mode 100644 v_windows/v/vlib/rand/wyrand/wyrand_test.v (limited to 'v_windows/v/vlib/rand/wyrand') diff --git a/v_windows/v/vlib/rand/wyrand/wyrand.v b/v_windows/v/vlib/rand/wyrand/wyrand.v new file mode 100644 index 0000000..22b71af --- /dev/null +++ b/v_windows/v/vlib/rand/wyrand/wyrand.v @@ -0,0 +1,252 @@ +// Copyright (c) 2019-2021 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 wyrand + +import math.bits +import rand.seed +import rand.constants +import hash + +// Redefinition of some constants that we will need for pseudorandom number generation. +const ( + wyp0 = u64(0xa0761d6478bd642f) + wyp1 = u64(0xe7037ed1a0b428db) +) + +// WyRandRNG is a RNG based on the WyHash hashing algorithm. +pub struct WyRandRNG { +mut: + state u64 = seed.time_seed_64() + has_extra bool + extra u32 +} + +// seed sets the seed, needs only two `u32`s in little-endian format as [lower, higher]. +pub fn (mut rng WyRandRNG) seed(seed_data []u32) { + if seed_data.len != 2 { + eprintln('WyRandRNG needs 2 32-bit unsigned integers as the seed.') + exit(1) + } + rng.state = seed_data[0] | (u64(seed_data[1]) << 32) + rng.has_extra = false +} + +// u32 updates the PRNG state and returns the next pseudorandom `u32`. +[inline] +pub fn (mut rng WyRandRNG) u32() u32 { + if rng.has_extra { + rng.has_extra = false + return rng.extra + } + full_value := rng.u64() + lower := u32(full_value & constants.lower_mask) + upper := u32(full_value >> 32) + rng.extra = upper + rng.has_extra = true + return lower +} + +// u64 updates the PRNG state and returns the next pseudorandom `u64`. +[inline] +pub fn (mut rng WyRandRNG) u64() u64 { + unsafe { + mut seed1 := rng.state + seed1 += wyrand.wyp0 + rng.state = seed1 + return hash.wymum(seed1 ^ wyrand.wyp1, seed1) + } + return 0 +} + +// u32n returns a pseudorandom `u32` less than `max`. +[inline] +pub fn (mut rng WyRandRNG) u32n(max u32) u32 { + if max == 0 { + eprintln('max must be positive integer') + exit(1) + } + // Check SysRNG in system_rng.c.v for explanation + bit_len := bits.len_32(max) + if bit_len == 32 { + for { + value := rng.u32() + if value < max { + return value + } + } + } else { + mask := (u32(1) << (bit_len + 1)) - 1 + for { + value := rng.u32() & mask + if value < max { + return value + } + } + } + return u32(0) +} + +// u64n returns a pseudorandom `u64` less than `max`. +[inline] +pub fn (mut rng WyRandRNG) u64n(max u64) u64 { + if max == 0 { + eprintln('max must be positive integer') + exit(1) + } + bit_len := bits.len_64(max) + if bit_len == 64 { + for { + value := rng.u64() + if value < max { + return value + } + } + } else { + mask := (u64(1) << (bit_len + 1)) - 1 + for { + value := rng.u64() & mask + if value < max { + return value + } + } + } + return u64(0) +} + +// u32n returns a pseudorandom `u32` value that is guaranteed to be in range `[min, max)`. +[inline] +pub fn (mut rng WyRandRNG) u32_in_range(min u32, max u32) u32 { + if max <= min { + eprintln('max must be greater than min') + exit(1) + } + return min + rng.u32n(max - min) +} + +// u64n returns a pseudorandom `u64` value that is guaranteed to be in range `[min, max)`. +[inline] +pub fn (mut rng WyRandRNG) u64_in_range(min u64, max u64) u64 { + if max <= min { + eprintln('max must be greater than min') + exit(1) + } + return min + rng.u64n(max - min) +} + +// int returns a (possibly negative) pseudorandom 32-bit `int`. +[inline] +pub fn (mut rng WyRandRNG) int() int { + return int(rng.u32()) +} + +// i64 returns a (possibly negative) pseudorandom 64-bit `i64`. +[inline] +pub fn (mut rng WyRandRNG) i64() i64 { + return i64(rng.u64()) +} + +// int31 returns a positive pseudorandom 31-bit `int`. +[inline] +pub fn (mut rng WyRandRNG) int31() int { + return int(rng.u32() & constants.u31_mask) // Set the 32nd bit to 0. +} + +// int63 returns a positive pseudorandom 63-bit `i64`. +[inline] +pub fn (mut rng WyRandRNG) int63() i64 { + return i64(rng.u64() & constants.u63_mask) // Set the 64th bit to 0. +} + +// intn returns a pseudorandom `int` in range `[0, max)`. +[inline] +pub fn (mut rng WyRandRNG) intn(max int) int { + if max <= 0 { + eprintln('max has to be positive.') + exit(1) + } + return int(rng.u32n(u32(max))) +} + +// i64n returns a pseudorandom int that lies in `[0, max)`. +[inline] +pub fn (mut rng WyRandRNG) i64n(max i64) i64 { + if max <= 0 { + eprintln('max has to be positive.') + exit(1) + } + return i64(rng.u64n(u64(max))) +} + +// int_in_range returns a pseudorandom `int` in range `[min, max)`. +[inline] +pub fn (mut rng WyRandRNG) int_in_range(min int, max int) int { + if max <= min { + eprintln('max must be greater than min') + exit(1) + } + // This supports negative ranges like [-10, -5) because the difference is positive + return min + rng.intn(max - min) +} + +// i64_in_range returns a pseudorandom `i64` in range `[min, max)`. +[inline] +pub fn (mut rng WyRandRNG) i64_in_range(min i64, max i64) i64 { + if max <= min { + eprintln('max must be greater than min') + exit(1) + } + return min + rng.i64n(max - min) +} + +// f32 returns a pseudorandom `f32` value in range `[0, 1)`. +[inline] +pub fn (mut rng WyRandRNG) f32() f32 { + return f32(rng.u32()) / constants.max_u32_as_f32 +} + +// f64 returns a pseudorandom `f64` value in range `[0, 1)`. +[inline] +pub fn (mut rng WyRandRNG) f64() f64 { + return f64(rng.u64()) / constants.max_u64_as_f64 +} + +// f32n returns a pseudorandom `f32` value in range `[0, max)`. +[inline] +pub fn (mut rng WyRandRNG) f32n(max f32) f32 { + if max <= 0 { + eprintln('max has to be positive.') + exit(1) + } + return rng.f32() * max +} + +// f64n returns a pseudorandom `f64` value in range `[0, max)`. +[inline] +pub fn (mut rng WyRandRNG) f64n(max f64) f64 { + if max <= 0 { + eprintln('max has to be positive.') + exit(1) + } + return rng.f64() * max +} + +// f32_in_range returns a pseudorandom `f32` in range `[min, max)`. +[inline] +pub fn (mut rng WyRandRNG) f32_in_range(min f32, max f32) f32 { + if max <= min { + eprintln('max must be greater than min') + exit(1) + } + return min + rng.f32n(max - min) +} + +// i64_in_range returns a pseudorandom `i64` in range `[min, max)`. +[inline] +pub fn (mut rng WyRandRNG) f64_in_range(min f64, max f64) f64 { + if max <= min { + eprintln('max must be greater than min') + exit(1) + } + return min + rng.f64n(max - min) +} diff --git a/v_windows/v/vlib/rand/wyrand/wyrand_test.v b/v_windows/v/vlib/rand/wyrand/wyrand_test.v new file mode 100644 index 0000000..4cdfdb6 --- /dev/null +++ b/v_windows/v/vlib/rand/wyrand/wyrand_test.v @@ -0,0 +1,331 @@ +import math +import rand.seed +import rand.wyrand + +const ( + range_limit = 40 + value_count = 1000 + seeds = [[u32(42), 0], [u32(256), 0]] +) + +const ( + sample_size = 1000 + stats_epsilon = 0.05 + inv_sqrt_12 = 1.0 / math.sqrt(12) +) + +fn gen_randoms(seed_data []u32, bound int) []u64 { + bound_u64 := u64(bound) + mut randoms := []u64{len: (20)} + mut rnd := wyrand.WyRandRNG{} + rnd.seed(seed_data) + for i in 0 .. 20 { + randoms[i] = rnd.u64n(bound_u64) + } + return randoms +} + +fn test_wyrand_reproducibility() { + seed_data := seed.time_seed_array(2) + randoms1 := gen_randoms(seed_data, 1000) + randoms2 := gen_randoms(seed_data, 1000) + assert randoms1.len == randoms2.len + len := randoms1.len + for i in 0 .. len { + assert randoms1[i] == randoms2[i] + } +} + +// TODO: use the `in` syntax and remove this function +// after generics has been completely implemented +fn found(value u64, arr []u64) bool { + for item in arr { + if value == item { + return true + } + } + return false +} + +fn test_wyrand_variability() { + // If this test fails and if it is certainly not the implementation + // at fault, try changing the seed values. Repeated values are + // improbable but not impossible. + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + mut values := []u64{cap: value_count} + for i in 0 .. value_count { + value := rng.u64() + assert !found(value, values) + assert values.len == i + values << value + } + } +} + +fn check_uniformity_u64(mut rng wyrand.WyRandRNG, range u64) { + range_f64 := f64(range) + expected_mean := range_f64 / 2.0 + mut variance := 0.0 + for _ in 0 .. sample_size { + diff := f64(rng.u64n(range)) - expected_mean + variance += diff * diff + } + variance /= sample_size - 1 + sigma := math.sqrt(variance) + expected_sigma := range_f64 * inv_sqrt_12 + error := (sigma - expected_sigma) / expected_sigma + assert math.abs(error) < stats_epsilon +} + +fn test_wyrand_uniformity_u64() { + ranges := [14019545, 80240, 130] + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for range in ranges { + check_uniformity_u64(mut rng, u64(range)) + } + } +} + +fn check_uniformity_f64(mut rng wyrand.WyRandRNG) { + expected_mean := 0.5 + mut variance := 0.0 + for _ in 0 .. sample_size { + diff := rng.f64() - expected_mean + variance += diff * diff + } + variance /= sample_size - 1 + sigma := math.sqrt(variance) + expected_sigma := inv_sqrt_12 + error := (sigma - expected_sigma) / expected_sigma + assert math.abs(error) < stats_epsilon +} + +fn test_wyrand_uniformity_f64() { + // The f64 version + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + check_uniformity_f64(mut rng) + } +} + +fn test_wyrand_u32n() { + max := u32(16384) + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for _ in 0 .. range_limit { + value := rng.u32n(max) + assert value >= 0 + assert value < max + } + } +} + +fn test_wyrand_u64n() { + max := u64(379091181005) + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for _ in 0 .. range_limit { + value := rng.u64n(max) + assert value >= 0 + assert value < max + } + } +} + +fn test_wyrand_u32_in_range() { + max := u32(484468466) + min := u32(316846) + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for _ in 0 .. range_limit { + value := rng.u32_in_range(min, max) + assert value >= min + assert value < max + } + } +} + +fn test_wyrand_u64_in_range() { + max := u64(216468454685163) + min := u64(6848646868) + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for _ in 0 .. range_limit { + value := rng.u64_in_range(min, max) + assert value >= min + assert value < max + } + } +} + +fn test_wyrand_int31() { + max_u31 := int(0x7FFFFFFF) + sign_mask := int(0x80000000) + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for _ in 0 .. range_limit { + value := rng.int31() + assert value >= 0 + assert value <= max_u31 + // This statement ensures that the sign bit is zero + assert (value & sign_mask) == 0 + } + } +} + +fn test_wyrand_int63() { + max_u63 := i64(0x7FFFFFFFFFFFFFFF) + sign_mask := i64(0x8000000000000000) + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for _ in 0 .. range_limit { + value := rng.int63() + assert value >= 0 + assert value <= max_u63 + assert (value & sign_mask) == 0 + } + } +} + +fn test_wyrand_intn() { + max := 2525642 + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for _ in 0 .. range_limit { + value := rng.intn(max) + assert value >= 0 + assert value < max + } + } +} + +fn test_wyrand_i64n() { + max := i64(3246727724653636) + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for _ in 0 .. range_limit { + value := rng.i64n(max) + assert value >= 0 + assert value < max + } + } +} + +fn test_wyrand_int_in_range() { + min := -4252 + max := 1034 + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for _ in 0 .. range_limit { + value := rng.int_in_range(min, max) + assert value >= min + assert value < max + } + } +} + +fn test_wyrand_i64_in_range() { + min := i64(-24095) + max := i64(324058) + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for _ in 0 .. range_limit { + value := rng.i64_in_range(min, max) + assert value >= min + assert value < max + } + } +} + +fn test_wyrand_f32() { + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for _ in 0 .. range_limit { + value := rng.f32() + assert value >= 0.0 + assert value < 1.0 + } + } +} + +fn test_wyrand_f64() { + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for _ in 0 .. range_limit { + value := rng.f64() + assert value >= 0.0 + assert value < 1.0 + } + } +} + +fn test_wyrand_f32n() { + max := f32(357.0) + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for _ in 0 .. range_limit { + value := rng.f32n(max) + assert value >= 0.0 + assert value < max + } + } +} + +fn test_wyrand_f64n() { + max := 1.52e6 + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for _ in 0 .. range_limit { + value := rng.f64n(max) + assert value >= 0.0 + assert value < max + } + } +} + +fn test_wyrand_f32_in_range() { + min := f32(-24.0) + max := f32(125.0) + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for _ in 0 .. range_limit { + value := rng.f32_in_range(min, max) + assert value >= min + assert value < max + } + } +} + +fn test_wyrand_f64_in_range() { + min := -548.7 + max := 5015.2 + for seed in seeds { + mut rng := wyrand.WyRandRNG{} + rng.seed(seed) + for _ in 0 .. range_limit { + value := rng.f64_in_range(min, max) + assert value >= min + assert value < max + } + } +} -- cgit v1.2.3