Repository navigation
Expand file tree
/
Copy pathmath_random.lua
More file actions
46 lines (39 loc) · 1.46 KB
/
Copy pathmath_random.lua
File metadata and controls
46 lines (39 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-- math.random / math.randomseed — xoshiro256** PRNG.
-- Determinism: the same seed reproduces the same sequence.
math.randomseed(42)
local a1 = math.random(1, 100)
local a2 = math.random(1, 100)
local a3 = math.random(1, 100)
math.randomseed(42)
local b1 = math.random(1, 100)
local b2 = math.random(1, 100)
local b3 = math.random(1, 100)
print(a1 == b1, a2 == b2, a3 == b3) -- true true true
-- Range bounds are inclusive on both ends.
math.randomseed(1)
local in_range = true
for i = 1, 50 do
local x = math.random(1, 6)
if x < 1 or x > 6 then in_range = false end
end
print(in_range) -- true
-- 0-arg form returns a float in [0, 1).
math.randomseed(7)
local f = math.random()
print(f >= 0 and f < 1) -- true
print(type(f)) -- number
print(math.type(f)) -- float
-- 1-arg form: math.random(n) returns an integer in [1, n].
math.randomseed(11)
local g = math.random(10)
print(g >= 1 and g <= 10) -- true
print(math.type(g)) -- integer
-- randomseed returns the seeds it actually used.
print(math.randomseed(123, 456)) -- 123 456
-- math.random(0) returns any 64-bit integer (full-range mode).
math.randomseed(0)
local r = math.random(0)
print(math.type(r)) -- integer
-- m > n is an error.
local ok = pcall(function() return math.random(10, 1) end)
print(ok) -- false