Random.new
func Random.new(seed: int | null = null): RandomRandom.new returns a generator seeded with seed. Without a seed, or with null, it is seeded from the host's secure source, as Crypto.randomBytes is, and its values differ from run to run.
Details
- The same seed gives the same values in the same order, on every run and every implementation of Ant. Any
intis a seed, including0and negative numbers. - A generator is not fit for keys or tokens. Use Crypto.randomBytes for those.
Basic examples
Make a generator from a seed and draw from it:
let r = Random.new(42)
r.int(0, 100) // 22
r.int(0, 100) // 33Without a seed, the generator starts somewhere new on every run:
let r = Random.new()
println(r.int(1, 7))Scope
Two generators made from one seed give the same sequence:
let a = Random.new(7)
let b = Random.new(7)
a.int(0, 1000) // 602
b.int(0, 1000) // 602Zero and negative numbers are seeds like any other:
Random.new(0).int(0, 100) // 52
Random.new(-1).int(0, 100) // 8null asks for a seed from the host, so an int | null passes through. Here the seed comes from the command line when one is given:
let seed = Os.args().get(0)?.toInt()
let r = Random.new(seed)
println(r.int(0, 100))22A generator renders as Random, never as its state:
Random.new(42) // RandomNamed arguments
seed
Name the seed to make its meaning plain at the call:
let dice = Random.new(seed: 2024)
[dice.int(1, 7), dice.int(1, 7), dice.int(1, 7)] // [6, 2, 4]Applications
Shuffle a deck so a test can check the order, since the seed fixes the shuffle:
let deck = ["A", "K", "Q", "J", "10", "9"]
Random.new(1).shuffle(deck)
deck // ["K", "J", "10", "A", "Q", "9"]Make repeatable IDs for tests by passing a seeded generator to the code that makes IDs:
let alphabet = "0123456789abcdefghjkmnpqrstvwxyz".chars()
func testId(r: Random): string {
Array.range(0, 6)
| Array.map({ alphabet[r.int(0, alphabet.length())] })
| String.join("")
}
let r = Random.new(42)
println([testId(r), testId(r)])["py114r", "j7yxh5"]These IDs are predictable. For keys or tokens, use Crypto.randomBytes.
Estimate pi with a Monte Carlo simulation. The fixed seed makes the estimate the same on every run:
let r = Random.new(5)
let n = 10000
var inside = 0
for _ in Iter.range(0, n) {
let x = r.float()
let y = r.float()
if x * x + y * y < 1.0 {
inside += 1
}
}
println(4.0 * float64(inside) / float64(n))3.1476Draw a fresh seed, print it, and build the generator from it. When a run goes wrong, passing the printed seed back to Random.new gives the same draws again:
let seed = Math.randomInt(0, 1_000_000)
println("seed ${seed}")
let r = Random.new(seed)
println(r.int(1, 7))Properties and relations
The same seed gives the same draws:
let a = Random.new(99)
let b = Random.new(99)
let xs = Array.range(0, 10).map({ a.int(0, 1_000_000) })
let ys = Array.range(0, 10).map({ b.int(0, 1_000_000) })
assert(xs == ys)Generators compare by identity, so two generators with one seed are not equal, and a generator is equal to itself:
let a = Random.new(1)
a == Random.new(1) // false
a == a // truePossible issues
Making a generator inside a loop with a fixed seed starts it over on every pass, so every pass draws the same value. Make it once, before the loop:
let same = Array.range(0, 3).map({ Random.new(42).int(0, 100) })
same // [22, 22, 22]
let r = Random.new(42)
let varied = Array.range(0, 3).map({ r.int(0, 100) })
varied // [22, 33, 33]Goroutines that draw from one generator without a lock race. Give each its own with Random.fork.
See also
Random · Random.fork · Random.int · Random.float · Random.bool · Random.pick · Random.shuffle · Math.random · Crypto.randomBytes
