Fix up test to use RNGs
This commit is contained in:
parent
fc9a6f1416
commit
d0ebb66b30
@ -105,7 +105,7 @@ func main():
|
||||
>> ["A", "B", "C"]:sample(10, [1.0, 0.5, 0.0])
|
||||
|
||||
do:
|
||||
>> heap := [Int.random(1, 50) for _ in 10]
|
||||
>> heap := [random:int(1, 50) for _ in 10]
|
||||
>> heap:heapify()
|
||||
>> heap
|
||||
sorted := [:Int]
|
||||
@ -114,7 +114,7 @@ func main():
|
||||
>> sorted == sorted:sorted()
|
||||
= yes
|
||||
for _ in 10:
|
||||
heap:heap_push(Int.random(1, 50))
|
||||
heap:heap_push(random:int(1, 50))
|
||||
>> heap
|
||||
sorted = [:Int]
|
||||
while heap.length > 0:
|
||||
@ -172,29 +172,3 @@ func main():
|
||||
= !Int
|
||||
>> [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
|
||||
|
@ -50,7 +50,6 @@ func main():
|
||||
>> x:octal()
|
||||
= "0o173"
|
||||
|
||||
>> Int.random(1, 100)
|
||||
>> Int64.min
|
||||
= -9223372036854775808[64]
|
||||
>> Int64.max
|
||||
@ -87,8 +86,8 @@ func main():
|
||||
|
||||
do:
|
||||
for in 20:
|
||||
>> n := Int.random(-999999, 999999)
|
||||
>> d := Int.random(-999, 999)
|
||||
>> n := random:int(-999999, 999999)
|
||||
>> d := random:int(-999, 999)
|
||||
!! n=$n, d=$d:
|
||||
>> (n/d)*d + (n mod d) == n
|
||||
= yes
|
||||
|
@ -17,8 +17,6 @@ func main():
|
||||
>> Num.PI:format(precision=10)
|
||||
= "3.1415926536"
|
||||
|
||||
>> Num.random()
|
||||
|
||||
>> Num.INF
|
||||
= inf
|
||||
>> Num.INF:isinf()
|
||||
|
Loading…
Reference in New Issue
Block a user