• Subject: Re: Learning Lua, encountered a little problem.
• From: Volodymyr Bezobiuk <dedoogun@...>
• Date: Fri, 21 Mar 2014 19:37:54 +0200

You should call once math.randomseed() at the beginning before using math.random() with some kind of 'unpredictable' (different every program run) number as argument. math.randomseed(os.time()) does
the trick, usually...

On Fri, Mar 21, 2014 at 6:46 PM, frants85 wrote:
Hello, I became interrested in Lua and wanted to learn it. I came from Python and Ruby.
To learn the new language I have decided to make a small program, a roulette game, but I've encountered a small problem. My randomize function always returns the number 31, no matter what.
Here's the whole code. Thanks for any comments, I'm still learning and not too familiar with this language.

print("Welcome to the Lua Roulette.")
repeat
print("Are you ready to start the game? (y/n)")
end

function sleep(t)
--sleep function, to make the game more dramatic
os.execute("sleep " .. tonumber(t))
end

function randomize(r)
--returns a random number, always returns 31 no matter what.
print("The wheel is spinning...")
sleep(2)
r = math.random(36)
print("The ball has landed on the number " .. r .. ".\n")
return r
end

function calculate(r, money, bet, loc)
--This function is not finished yet
if type(tonumber(bet)) == "number" then
if loc == r then
money = money + (35 + bet)
return money
end
elseif loc == "manque" then
if r <= 1 and r >= 18 then
money = money + ((36 / 18) + bet)
return money
end
elseif loc == "passe" then
if r <= 19 and r >= 36 then
money = money + ((36 / 18) + bet)
return money
end
end
end

function game()
wheel = 0
chips = 150
bet = ""
betLoc = "" -- on which number did the player bet
repeat
print("Place your bets! You can bet on numbers((1-36)type 'number'), Manque(1-18),\n")
print(" Passe(19-36), red, black, even, odd or quit(q).\n")
print("Your current balance is " .. "\$" .. tostring(chips))
if betLoc == "q" then
os.exit()
else
print("How many do you want to bet?\n")
bet = tonumber(bet)
chips = chips - bet
randomize(wheel)
calculate(wheel, chips, bet, betLoc)
end
until chips == 0
print("Sorry, you lost all your money.")
print("Do you want to play again? (y/n)\n")
--ask if the player wants to play again
if playagain == 'y' then
else
os.exit()
end
end