I am having trouble with the math.random() function in Lua. The code I'm trying to run is:
for x = 1,5 do
math.randomseed(os.time())
math.random(); math.random(); math.random()
value = math.random(0,9)
print(value)
end
The random number that is being printed is always the same.
What can be the possible solution to this? I want 5 unique random numbers.
Initialize random once (outside the loop), use many:
math.randomseed(os.time()) -- random initialize
math.random(); math.random(); math.random() -- warming up
for x = 1,5 do
-- random generating
value = math.random(0,9)
print(value)
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With