diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-11-03 16:10:03 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-11-03 16:10:03 -0500 |
| commit | 7ccb7a8a9b8f10d30fd8fca01e849dcad354a855 (patch) | |
| tree | 1f1bf3c3617f790f80598fa82b8fadafc576e853 /test | |
| parent | 39a58bc129fd9461d54b837bc1650c4c650aa333 (diff) | |
Use an RNG parameter for array:random(), array:shuffle(),
array:shuffled()
Diffstat (limited to 'test')
| -rw-r--r-- | test/arrays.tm | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/test/arrays.tm b/test/arrays.tm index e1c31cf6..25d45582 100644 --- a/test/arrays.tm +++ b/test/arrays.tm @@ -173,3 +173,28 @@ func main(): >> [4, 5, 6]:first(func(i:&Int): i:is_prime()) = 2? + test_seeded_rng() + +# Inspired by: https://nullprogram.com/blog/2017/09/21/ +struct RNGxoroshiro128(s0=Int64.random(),s1=Int64.random()): + func int_fn(rng:@RNGxoroshiro128 -> func(->Int64)): + return func(-> Int64): + s0 := rng.s0 + s1 := rng.s1 + result := s0:wrapping_plus(s1) + s1 xor= s0 + rng.s0 = (s0 <<< 55) or (s0 >>> 9) xor s1 xor (s1 <<< 14) + rng.s1 = (s1 <<< 36) or (s1 >>> 28) + return result + +func test_seeded_rng(): + !! Seeded RNG: + rng_state := RNGxoroshiro128(Int64.random(), Int64.random()) + rng1 := @rng_state:int_fn() + rng2 := @rng_state:int_fn() + + nums := [i*10 for i in 20] + >> nums:random(rng1) == nums:random(rng2) + = yes + >> nums:shuffled(rng1) == nums:shuffled(rng2) + = yes |
