4.78. kink/random/RNG¶
Random number generators.
4.78.1. type rng¶
Abstract type of random number generator.
4.78.1.1. Rng.gen_int(Upper_bound)¶
`gen_int` produces a random int num in the range [0, Upper_bound).
Preconditions:
• `Upper_bound` must be an int num
• `Upper_bound` must be greater than or equal to 1
Every int nums in the range should be produced with approximately equal probability.
Example:
:PRNG.require_from('kink/random/')
:VEC.require_from('kink/')
:Rng <- PRNG.from_seed(42)
:Nums <- VEC.from_each(10.times.map{ Rng.gen_int(3) })
stdout.print_line(Nums.repr)
# => [2 0 2 2 1 0 2 1 2 2]
4.78.1.2. Rng.gen_bin(Size)¶
`gen_bin` produces a bin of the `Size` bits of which is randomly determined.
Preconditions:
• `Size` must be a non-negative int num
Probability of `1` bits and `0` bits should be approximately equal.
Example:
:PRNG.require_from('kink/random/')
:Rng <- PRNG.from_seed(42)
stdout.print_line(Rng.gen_bin(10).repr)
# => BIN.of(0x16 0x22 0xad 0x5f 0xe9 0xb6 0x94 0x4d 0x98 0xb1)
4.78.1.3. Rng.gen_bool¶
`gen_bool` produces a random bool.
Probability of true and false should be approximately equal.
Example:
:PRNG.require_from('kink/random/')
:VEC.require_from('kink/')
:Rng <- PRNG.from_seed(42)
:Bools <- VEC.from_each(10.times.map{ Rng.gen_bool })
stdout.print_line(Bools.repr)
# => [false true true false true true false false false true]
4.78.2. RNG.is?(Val)¶
`is?` returns whether `Val` is an rng val.