so I'm new to LUA and am writing a simple guess-the-number script, but I've found a weird quirk that happens with math.random
and I would like to understand what's happening here.
So I create a random seed with math.randomseed(os.time())
, but when I go to get a random number, like this:
correctNum = math.random(10)
print(correctNum)
,
it always gets the same random number everytime I run it, unless I do it twice (irrespective of arguments given):
random1 = math.random(10)
print(random1)
,
random2 = math.random(10)
print(random2)
in which case the first random number will never reroll on every execution, but the second one will.
Just confused about how randomization works in LUA and would appreciate some help.
Thanks,
-Electroshockist
Here is the full working code:
math.randomseed(os.time())
random1 = math.random(10)
print(random1)
random2 = math.random(10)
print(random2)
repeat
io.write "\nEnter your guess between 1 and 10: "
guess = io.read()
if tonumber(guess) ~= random2 then
print("Try again!")
end
print()
until tonumber(guess) == random2
print("Correct!")