mirror of
https://github.com/janet-lang/janet
synced 2025-11-03 00:53:01 +00:00
Add RNG functionality to the math/ module.
The new RNG wraps up state for random number generation, so one can have many rngs and even marshal and unmarshal them. Adds math/rng, math/rng-uniform, and math/rng-int. Also introduce `in` and change semantics for indexing out of range. This commit enforces stricter invariants on keys when indexing via a function call on the data structure, or the new `in` function. The `get` function is now more lax about keys, and will not throw an error when a bad key is used for a data structure, instead returning the default value.
This commit is contained in:
@@ -193,4 +193,19 @@
|
||||
# Trim empty string
|
||||
(assert (= "" (string/trim " ")) "string/trim regression")
|
||||
|
||||
# RNGs
|
||||
|
||||
(defn test-rng
|
||||
[rng]
|
||||
(assert (all identity (seq [i :range [0 1000]]
|
||||
(<= (math/rng-int rng i) i))) "math/rng-int test")
|
||||
(assert (all identity (seq [i :range [0 1000]]
|
||||
(def x (math/rng-uniform rng))
|
||||
(and (>= x 0) (< x 1))))
|
||||
"math/rng-uniform test"))
|
||||
|
||||
(def seedrng (math/rng 123))
|
||||
(for i 0 75
|
||||
(test-rng (math/rng (:int seedrng))))
|
||||
|
||||
(end-suite)
|
||||
|
||||
Reference in New Issue
Block a user