Random.bool
func Random.bool(r: Random): boolRandom.bool returns true or false, each half the time.
Details
- Each call takes one draw from
rand so changes it; the next call can give a different value. - A generator made with the same seed gives the same values in the same order, on every run.
- It is not fit for secrets. Use Crypto.randomBytes for those.
Basic examples
Make a generator with Random.new and flip a coin with it:
let r = Random.new(42)
Random.bool(r) // false
Random.bool(r) // false
Random.bool(r) // trueCall it through . on the generator:
let r = Random.new(7)
r.bool() // true
r.bool() // falseScope
A generator without a seed gives values that differ from run to run:
let r = Random.new()
println(r.bool())Call it in a lambda to make an array of flips:
let r = Random.new(42)
Array.range(0, 8) | Array.map({ r.bool() }) // [false, false, true, true, true, true, true, true]Random.bool is a function value of type func(Random): bool:
let flip: func(Random): bool = Random.bool
flip(Random.new(42)) // falseApplications
Name the sides of a coin with an if expression:
let coin = Random.new(2024)
let sides = Array.range(0, 6) | Array.map({ if coin.bool() { "heads" } else { "tails" } })
sides | String.join(" ") // "tails heads tails tails heads tails"Take a random walk, one step left or right at a time:
let r = Random.new(5)
var position = 0
for _ in Iter.range(0, 10) {
position += if r.bool() { 1 } else { -1 }
}
println(position)4Give each number a random sign:
let r = Random.new(9)
Array.range(0, 5) | Array.map({ if r.bool() { x } else { -x } }) // [0, -1, -2, 3, 4]Upper-case each letter on a coin flip:
let r = Random.new(11)
let mixed = "ant colony".chars()
| Array.map({ if r.bool() { x.toUpper() } else { x } })
| String.join("")
println(mixed)ant coLONyProperties and relations
The same seed gives the same flips:
let a = Random.new(7)
let b = Random.new(7)
for _ in Iter.range(0, 100) {
assert(a.bool() == b.bool())
}Random.bool takes exactly one draw, as Random.float does, so two generators with one seed stay in step after either call:
let a = Random.new(42)
let b = Random.new(42)
a.bool()
b.float()
assert(a.int(0, 1000) == b.int(0, 1000))true comes up about half the time:
let r = Random.new(1)
let heads = Array.range(0, 10_000) | Array.countWhere({ r.bool() })
assert(heads > 4800 && heads < 5200)Possible issues
A new generator with the same seed for every flip gives the same flip every time. Make the generator once and draw from it:
Random.bool(Random.new(42)) // false
Random.bool(Random.new(42)) // falseRandom.bool takes no probability, so a second argument is a compile error:
let rare = Random.bool(Random.new(1), 0.3) // error: Random.bool takes 1 argument, got 2For odds other than one half, compare Random.float with the probability:
let r = Random.new(1)
let rare = Array.range(0, 10_000) | Array.countWhere({ r.float() < 0.3 })
assert(rare > 2800 && rare < 3200)A call without a generator is a compile error too. For an unseeded coin, make one with Random.new():
let heads = Random.bool() // error: Random.bool needs an argument for rSee also
Random.new · Random.float · Random.int · Random.pick · Math.random · Random namespace
