Math.random
func Math.random(): float64Math.random returns a uniformly random number in [0, 1). The sequence cannot be seeded; a Random generator is the seeded form.
Details
- The values differ from run to run.
- It is not fit for keys or tokens. Use Crypto.randomBytes for those.
Basic examples
Draw a number:
println(Math.random())The number is in [0, 1):
let x = Math.random()
x >= 0.0 && x < 1.0 // trueScope
Math.random takes no arguments. As a function value its type is func(): float64:
let draw: func(): float64 = Math.random
draw() < 1.0 // trueCall it in a lambda to make an array of draws:
let xs = Array.range(0, 5).map({ Math.random() })
xs.length() // 5
xs.every({ x >= 0.0 && x < 1.0 }) // trueApplications
Scale the number to a range of floats, here [-1, 1):
func between(lo: float64, hi: float64): float64 {
lo + Math.random() * (hi - lo)
}
let v = between(-1.0, 1.0)
v >= -1.0 && v < 1.0 // trueDo something with probability p. A p of 0.0 never happens, and 1.0 always does:
func chance(p: float64): bool {
Math.random() < p
}
chance(0.0) // false
chance(1.0) // trueSpread out retries, so that clients that failed together do not retry together:
func backoff(attempt: int): float64 {
let base = 100.0 * Math.pow(2.0, float64(attempt))
base + Math.random() * base
}
let delay = backoff(3)
delay >= 800.0 && delay < 1600.0 // truePick an item by weight:
type Prize = { name: string, weight: float64 }
func draw(prizes: Array[Prize]): string {
var total = 0.0
for p in prizes {
total += p.weight
}
var roll = Math.random() * total
for p in prizes {
if roll < p.weight {
return p.name
}
roll -= p.weight
}
prizes[prizes.length() - 1].name
}
let prizes = [Prize{ name: "gold", weight: 1.0 }, Prize{ name: "silver", weight: 3.0 }]
["gold", "silver"].contains(draw(prizes)) // trueEstimate π from the share of random points that fall inside a quarter circle:
let n = 100_000
var inside = 0
for _ in Iter.range(0, n) {
let x = Math.random()
let y = Math.random()
if x * x + y * y < 1.0 {
inside += 1
}
}
println(4.0 * float64(inside) / float64(n))Properties and relations
Every draw is at least 0.0 and below 1.0:
for _ in Iter.range(0, 1000) {
let x = Math.random()
assert(x >= 0.0 && x < 1.0)
}Scaling by n and taking Math.floor gives a whole number below n; for an int, Math.randomInt does this directly:
for _ in Iter.range(0, 1000) {
let i = int(Math.floor(Math.random() * 6.0))
assert(i >= 0 && i < 6)
let j = Math.randomInt(0, 6)
assert(j >= 0 && j < 6)
}Random.float gives numbers in the same range, and the same ones for the same seed:
let a = Random.new(42)
let b = Random.new(42)
a.float() // 0.08386297105988216
b.float() // 0.08386297105988216Possible issues
Math.random cannot be seeded, so a seed is a compile error. Use Random.new for a seeded generator:
let x = Math.random(42) // error: Math.random takes 0 arguments, got 1The result is a float64, and an int literal does not become one. Write 10.0, not 10:
let x = Math.random() * 10 // error: * needs both operands of one type, got float64 and intlet x = Math.random() * 10.0
x < 10.0 // trueMath.round of a scaled draw makes the two ends half as likely as the numbers between them. For a uniform int, use Math.randomInt:
let die = Math.randomInt(1, 7)
die >= 1 && die <= 6 // trueSee also
Math.randomInt · Random · Random.float · Crypto.randomBytes · Math namespace
