aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/vlib/rand/mt19937
diff options
context:
space:
mode:
authorIndrajith K L2022-12-03 17:00:20 +0530
committerIndrajith K L2022-12-03 17:00:20 +0530
commitf5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch)
tree2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/vlib/rand/mt19937
downloadcli-tools-windows-master.tar.gz
cli-tools-windows-master.tar.bz2
cli-tools-windows-master.zip
Adds most of the toolsHEADmaster
Diffstat (limited to 'v_windows/v/vlib/rand/mt19937')
-rw-r--r--v_windows/v/vlib/rand/mt19937/mt19937.v325
-rw-r--r--v_windows/v/vlib/rand/mt19937/mt19937_test.v341
2 files changed, 666 insertions, 0 deletions
diff --git a/v_windows/v/vlib/rand/mt19937/mt19937.v b/v_windows/v/vlib/rand/mt19937/mt19937.v
new file mode 100644
index 0000000..256fc06
--- /dev/null
+++ b/v_windows/v/vlib/rand/mt19937/mt19937.v
@@ -0,0 +1,325 @@
+// 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 mt19937
+
+import math.bits
+
+/*
+C++ functions for MT19937, with initialization improved 2002/2/10.
+ Coded by Takuji Nishimura and Makoto Matsumoto.
+ This is a faster version by taking Shawn Cokus's optimization,
+ Matthe Bellew's simplification, Isaku Wada's real version.
+
+ Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ 3. The names of its contributors may not be used to endorse or promote
+ products derived from this software without specific prior written
+ permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+ Any feedback is very welcome.
+ http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
+ email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
+*/
+const (
+ nn = 312
+ mm = 156
+ matrix_a = 0xB5026F5AA96619E9
+ um = 0xFFFFFFFF80000000
+ lm = 0x7FFFFFFF
+ inv_f64_limit = 1.0 / 9007199254740992.0
+)
+
+// MT19937RNG is generator that uses the Mersenne Twister algorithm with period 2^19937.
+// **NOTE**: The RNG is not seeded when instantiated so remember to seed it before use.
+pub struct MT19937RNG {
+mut:
+ state []u64 = []u64{len: mt19937.nn}
+ mti int = mt19937.nn
+ next_rnd u32
+ has_next bool
+}
+
+// calculate_state returns a random state array calculated from the `seed_data`.
+fn calculate_state(seed_data []u32, mut state []u64) []u64 {
+ lo := u64(seed_data[0])
+ hi := u64(seed_data[1])
+ state[0] = u64((hi << 32) | lo)
+ for j := 1; j < mt19937.nn; j++ {
+ state[j] = u64(6364136223846793005) * (state[j - 1] ^ (state[j - 1] >> 62)) + u64(j)
+ }
+ return *state
+}
+
+// seed sets the current random state based on `seed_data`.
+// seed expects `seed_data` to be only two `u32`s in little-endian format as [lower, higher].
+pub fn (mut rng MT19937RNG) seed(seed_data []u32) {
+ if seed_data.len != 2 {
+ eprintln('mt19937 needs only two 32bit integers as seed: [lower, higher]')
+ exit(1)
+ }
+ // calculate 2 times because MT19937RNG init didn't call calculate_state.
+ rng.state = calculate_state(seed_data, mut rng.state)
+ rng.state = calculate_state(seed_data, mut rng.state)
+ rng.mti = mt19937.nn
+ rng.next_rnd = 0
+ rng.has_next = false
+}
+
+// u32 returns a pseudorandom 32bit int in range `[0, 2³²)`.
+[inline]
+pub fn (mut rng MT19937RNG) u32() u32 {
+ if rng.has_next {
+ rng.has_next = false
+ return rng.next_rnd
+ }
+ ans := rng.u64()
+ rng.next_rnd = u32(ans >> 32)
+ rng.has_next = true
+ return u32(ans & 0xffffffff)
+}
+
+// u64 returns a pseudorandom 64bit int in range `[0, 2⁶⁴)`.
+[inline]
+pub fn (mut rng MT19937RNG) u64() u64 {
+ mag01 := [u64(0), u64(mt19937.matrix_a)]
+ mut x := u64(0)
+ mut i := int(0)
+ if rng.mti >= mt19937.nn {
+ for i = 0; i < mt19937.nn - mt19937.mm; i++ {
+ x = (rng.state[i] & mt19937.um) | (rng.state[i + 1] & mt19937.lm)
+ rng.state[i] = rng.state[i + mt19937.mm] ^ (x >> 1) ^ mag01[int(x & 1)]
+ }
+ for i < mt19937.nn - 1 {
+ x = (rng.state[i] & mt19937.um) | (rng.state[i + 1] & mt19937.lm)
+ rng.state[i] = rng.state[i + (mt19937.mm - mt19937.nn)] ^ (x >> 1) ^ mag01[int(x & 1)]
+ i++
+ }
+ x = (rng.state[mt19937.nn - 1] & mt19937.um) | (rng.state[0] & mt19937.lm)
+ rng.state[mt19937.nn - 1] = rng.state[mt19937.mm - 1] ^ (x >> 1) ^ mag01[int(x & 1)]
+ rng.mti = 0
+ }
+ x = rng.state[rng.mti]
+ rng.mti++
+ x ^= (x >> 29) & 0x5555555555555555
+ x ^= (x << 17) & 0x71D67FFFEDA60000
+ x ^= (x << 37) & 0xFFF7EEE000000000
+ x ^= (x >> 43)
+ return x
+}
+
+// int returns a 32-bit signed (possibly negative) `int`.
+[inline]
+pub fn (mut rng MT19937RNG) int() int {
+ return int(rng.u32())
+}
+
+// i64 returns a 64-bit signed (possibly negative) `i64`.
+[inline]
+pub fn (mut rng MT19937RNG) i64() i64 {
+ return i64(rng.u64())
+}
+
+// int31 returns a 31bit positive pseudorandom `int`.
+[inline]
+pub fn (mut rng MT19937RNG) int31() int {
+ return int(rng.u32() >> 1)
+}
+
+// int63 returns a 63bit positive pseudorandom `i64`.
+[inline]
+pub fn (mut rng MT19937RNG) int63() i64 {
+ return i64(rng.u64() >> 1)
+}
+
+// u32n returns a 32bit `u32` in range `[0, max)`.
+[inline]
+pub fn (mut rng MT19937RNG) 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 64bit `u64` in range `[0, max)`.
+[inline]
+pub fn (mut rng MT19937RNG) 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 MT19937RNG) 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 MT19937RNG) 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)
+}
+
+// intn returns a 32bit positive `int` in range `[0, max)`.
+[inline]
+pub fn (mut rng MT19937RNG) intn(max int) int {
+ if max <= 0 {
+ eprintln('max has to be positive.')
+ exit(1)
+ }
+ return int(rng.u32n(u32(max)))
+}
+
+// i64n returns a 64bit positive `i64` in range `[0, max)`.
+[inline]
+pub fn (mut rng MT19937RNG) 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 32bit positive `int` in range `[min, max)`.
+[inline]
+pub fn (mut rng MT19937RNG) int_in_range(min int, max int) int {
+ if max <= min {
+ eprintln('max must be greater than min.')
+ exit(1)
+ }
+ return min + rng.intn(max - min)
+}
+
+// i64_in_range returns a 64bit positive `i64` in range `[min, max)`.
+[inline]
+pub fn (mut rng MT19937RNG) 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 32bit real (`f32`) in range `[0, 1)`.
+[inline]
+pub fn (mut rng MT19937RNG) f32() f32 {
+ return f32(rng.f64())
+}
+
+// f64 returns 64bit real (`f64`) in range `[0, 1)`.
+[inline]
+pub fn (mut rng MT19937RNG) f64() f64 {
+ return f64(rng.u64() >> 11) * mt19937.inv_f64_limit
+}
+
+// f32n returns a 32bit real (`f32`) in range [0, max)`.
+[inline]
+pub fn (mut rng MT19937RNG) f32n(max f32) f32 {
+ if max <= 0 {
+ eprintln('max has to be positive.')
+ exit(1)
+ }
+ return rng.f32() * max
+}
+
+// f64n returns a 64bit real (`f64`) in range `[0, max)`.
+[inline]
+pub fn (mut rng MT19937RNG) 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` that lies in range `[min, max)`.
+[inline]
+pub fn (mut rng MT19937RNG) 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` that lies in range `[min, max)`.
+[inline]
+pub fn (mut rng MT19937RNG) 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/mt19937/mt19937_test.v b/v_windows/v/vlib/rand/mt19937/mt19937_test.v
new file mode 100644
index 0000000..f2a41de
--- /dev/null
+++ b/v_windows/v/vlib/rand/mt19937/mt19937_test.v
@@ -0,0 +1,341 @@
+import math
+import rand.mt19937
+import rand.seed
+
+const (
+ range_limit = 40
+ value_count = 1000
+ seeds = [[u32(0xcafebabe), u32(0xdeadbeef)], [u32(0xc0de), u32(0xfeed)]]
+)
+
+const (
+ sample_size = 1000
+ stats_epsilon = 0.05
+ inv_sqrt_12 = 1.0 / math.sqrt(12)
+)
+
+fn mt19937_basic_test() {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed([u32(0xdeadbeef)])
+ target := [u32(956529277), 3842322136, 3319553134, 1843186657, 2704993644, 595827513, 938518626,
+ 1676224337, 3221315650, 1819026461]
+ for i := 0; i < 10; i++ {
+ assert target[i] == rng.u32()
+ }
+}
+
+fn gen_randoms(seed_data []u32, bound int) []u64 {
+ bound_u64 := u64(bound)
+ mut randoms := []u64{len: (20)}
+ mut rnd := mt19937.MT19937RNG{}
+ rnd.seed(seed_data)
+ for i in 0 .. 20 {
+ randoms[i] = rnd.u64n(bound_u64)
+ }
+ return randoms
+}
+
+fn test_mt19937_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_mt19937_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 := mt19937.MT19937RNG{}
+ 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 mt19937.MT19937RNG, 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_mt19937_uniformity_u64() {
+ ranges := [14019545, 80240, 130]
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed(seed)
+ for range in ranges {
+ check_uniformity_u64(mut rng, u64(range))
+ }
+ }
+}
+
+fn check_uniformity_f64(mut rng mt19937.MT19937RNG) {
+ 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_mt19937_uniformity_f64() {
+ // The f64 version
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed(seed)
+ check_uniformity_f64(mut rng)
+ }
+}
+
+fn test_mt19937_u32n() {
+ max := u32(16384)
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed(seed)
+ for _ in 0 .. range_limit {
+ value := rng.u32n(max)
+ assert value >= 0
+ assert value < max
+ }
+ }
+}
+
+fn test_mt19937_u64n() {
+ max := u64(379091181005)
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed(seed)
+ for _ in 0 .. range_limit {
+ value := rng.u64n(max)
+ assert value >= 0
+ assert value < max
+ }
+ }
+}
+
+fn test_mt19937_u32_in_range() {
+ max := u32(484468466)
+ min := u32(316846)
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed(seed)
+ for _ in 0 .. range_limit {
+ value := rng.u32_in_range(min, max)
+ assert value >= min
+ assert value < max
+ }
+ }
+}
+
+fn test_mt19937_u64_in_range() {
+ max := u64(216468454685163)
+ min := u64(6848646868)
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed(seed)
+ for _ in 0 .. range_limit {
+ value := rng.u64_in_range(min, max)
+ assert value >= min
+ assert value < max
+ }
+ }
+}
+
+fn test_mt19937_int31() {
+ max_u31 := int(0x7FFFFFFF)
+ sign_mask := int(0x80000000)
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ 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_mt19937_int63() {
+ max_u63 := i64(0x7FFFFFFFFFFFFFFF)
+ sign_mask := i64(0x8000000000000000)
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ 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_mt19937_intn() {
+ max := 2525642
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed(seed)
+ for _ in 0 .. range_limit {
+ value := rng.intn(max)
+ assert value >= 0
+ assert value < max
+ }
+ }
+}
+
+fn test_mt19937_i64n() {
+ max := i64(3246727724653636)
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed(seed)
+ for _ in 0 .. range_limit {
+ value := rng.i64n(max)
+ assert value >= 0
+ assert value < max
+ }
+ }
+}
+
+fn test_mt19937_int_in_range() {
+ min := -4252
+ max := 1034
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed(seed)
+ for _ in 0 .. range_limit {
+ value := rng.int_in_range(min, max)
+ assert value >= min
+ assert value < max
+ }
+ }
+}
+
+fn test_mt19937_i64_in_range() {
+ min := i64(-24095)
+ max := i64(324058)
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed(seed)
+ for _ in 0 .. range_limit {
+ value := rng.i64_in_range(min, max)
+ assert value >= min
+ assert value < max
+ }
+ }
+}
+
+fn test_mt19937_f32() {
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed(seed)
+ for _ in 0 .. range_limit {
+ value := rng.f32()
+ assert value >= 0.0
+ assert value < 1.0
+ }
+ }
+}
+
+fn test_mt19937_f64() {
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed(seed)
+ for _ in 0 .. range_limit {
+ value := rng.f64()
+ assert value >= 0.0
+ assert value < 1.0
+ }
+ }
+}
+
+fn test_mt19937_f32n() {
+ max := f32(357.0)
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed(seed)
+ for _ in 0 .. range_limit {
+ value := rng.f32n(max)
+ assert value >= 0.0
+ assert value < max
+ }
+ }
+}
+
+fn test_mt19937_f64n() {
+ max := 1.52e6
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed(seed)
+ for _ in 0 .. range_limit {
+ value := rng.f64n(max)
+ assert value >= 0.0
+ assert value < max
+ }
+ }
+}
+
+fn test_mt19937_f32_in_range() {
+ min := f32(-24.0)
+ max := f32(125.0)
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed(seed)
+ for _ in 0 .. range_limit {
+ value := rng.f32_in_range(min, max)
+ assert value >= min
+ assert value < max
+ }
+ }
+}
+
+fn test_mt19937_f64_in_range() {
+ min := -548.7
+ max := 5015.2
+ for seed in seeds {
+ mut rng := mt19937.MT19937RNG{}
+ rng.seed(seed)
+ for _ in 0 .. range_limit {
+ value := rng.f64_in_range(min, max)
+ assert value >= min
+ assert value < max
+ }
+ }
+}