Math.randomInt
func Math.randomInt(from: int, to: int): intMath.randomInt returns a uniformly random int from from, inclusive, to to, exclusive. It draws from the same unseeded sequence as Math.random. It panics if to <= from.
Details
fromis a possible result andtois not:Math.randomInt(1, 7)gives one of 1 through 6.- The values differ from run to run, and nothing can seed them. For a sequence you can repeat, use Random.new with a seed and Random.int.
- Not fit for keys, tokens, or passwords. Use Crypto.randomBytes for those.
Basic examples
Roll a six-sided die. The result is one of 1 through 6:
let roll = Math.randomInt(1, 7)
roll >= 1 && roll <= 6 // truePick a random element of an array by drawing an index from 0 up to its length:
let colors = ["red", "green", "blue"]
let color = colors[Math.randomInt(0, len(colors))]
colors.contains(color) // trueScope
A range one wide has only one possible result:
Math.randomInt(5, 6) // 5
Math.randomInt(-3, -2) // -3Negative bounds work like any others:
let t = Math.randomInt(-10, 0)
t >= -10 && t < 0 // trueThe range may span every int but Math.maxInt:
let n = Math.randomInt(Math.minInt, Math.maxInt)
n < Math.maxInt // trueThe parameters are named from and to:
Math.randomInt(from: 0, to: 1) // 0In a pipe, the value on the left becomes from:
6 | Math.randomInt(7) // 6Applications
Flip a coin:
let side = if Math.randomInt(0, 2) == 0 { "heads" } else { "tails" }
side == "heads" || side == "tails" // trueRoll two dice and add them, which gives 2 through 12, with 7 the most likely:
func twoDice(): int {
Math.randomInt(1, 7) + Math.randomInt(1, 7)
}
for _ in Array.range(0, 1000) {
let sum = twoDice()
assert(sum >= 2 && sum <= 12)
}Add jitter to a retry delay, so that many clients do not retry at the same moment:
func retryDelayMs(attempt: int): int {
let base = 100 * (attempt + 1)
base + Math.randomInt(-base / 10, base / 10 + 1)
}
let d = retryDelayMs(4)
d >= 450 && d <= 550 // trueCount how often each face of a die comes up:
let counts = Array.filled(6, 0)
for _ in Array.range(0, 6000) {
let face = Math.randomInt(1, 7)
counts[face - 1] += 1
}
assert(counts.every({ x > 0 }))
assert(counts.sum() == 6000)Properties and relations
Every result is at least from and less than to:
for _ in Array.range(0, 1000) {
let x = Math.randomInt(10, 20)
assert(x >= 10 && x < 20)
}Adding from to a draw from zero gives the same range:
for _ in Array.range(0, 1000) {
let x = 10 + Math.randomInt(0, 10)
assert(x >= 10 && x < 20)
}A float from Math.random, scaled and cut down to an int, gives the same range:
for _ in Array.range(0, 1000) {
let x = int(Math.floor(Math.random() * 6.0)) + 1
assert(x >= 1 && x <= 6)
}Possible issues
to is excluded, so a die rolled with Math.randomInt(1, 6) never shows a 6:
for _ in Array.range(0, 1000) {
assert(Math.randomInt(1, 6) != 6)
}An empty range panics, and so does a range with its bounds swapped:
Math.randomInt(0, 0) // panic: Math.randomInt needs from < to, got 0 and 0
Math.randomInt(3, 1) // panic: Math.randomInt needs from < to, got 3 and 1Drawing an index for an empty array is the same empty range, so check the length first:
let empty: Array[string] = []
empty[Math.randomInt(0, len(empty))] // panic: Math.randomInt needs from < to, got 0 and 0The bounds are int. A float64 or another integer type is a compile error:
let x = Math.randomInt(1.0, 7) // error: cannot use float64 where int is expectedThe results cannot be repeated. For a test or a replayable game, seed a Random instead, which gives the same values on every run:
let r = Random.new(42)
r.int(0, 100) // 22See also
Math.random · Random.int · Random.pick · Random.shuffle · Crypto.randomBytes
