Page 1 of 2
Random Number Generator?
Posted: Tue Nov 19, 2013 3:31 am
by Op Winter Storm
Hey, I'm brand new to Lua. I was wondering if anyone could teach me how to make a simple random number generator.
Basically, define a list of numbers (say, 1-100), and maybe some more advanced stuff later.
Thanks!
Re: Random Number Generator?
Posted: Tue Nov 19, 2013 5:09 am
by Ensayia
Lua comes with the math.random() function. Have a look at
http://www.lua.org/pil/18.html
Re: Random Number Generator?
Posted: Tue Nov 19, 2013 10:30 am
by Teraku
Code: Select all
randomNumber = math.random(1, 100)
This assigns a random number between 1 and 100 to the randomNumber variable. No need to do any extra stuff to get a different number each time.
Re: Random Number Generator?
Posted: Tue Nov 19, 2013 8:53 pm
by Op Winter Storm
Ok, thanks!
So I would I set this up? I know that you have to have a main.lua, a conf.lua, and a play.bat, and that you need to enable/disable all of these things in conf.lua, and you have
function love.load ()
end
function love.draw ()
end
function love.update ()
end
in main.lua, and the stuff to put in play.bat, but where do you put the random generator part?
Re: Random Number Generator?
Posted: Tue Nov 19, 2013 9:06 pm
by Teraku
Put it anywhere you need. If you want more specific help, you need to tell us what you want. Here's some example code which prints a random number near the top left of the screen, and refreshes it every time you press "r":
Code: Select all
function love.load()
number = math.random(1, 100)
end
function love.draw()
love.graphics.print(number, 10, 10)
end
function love.keypressed(key)
if key == "r" then
number = math.random(1, 100)
end
end
Re: Random Number Generator?
Posted: Wed Nov 20, 2013 12:23 am
by Op Winter Storm
Thanks, this is really neat! But when I hit "r", it doesn't come up with a new number. Is there a way that I can make the number go after a word, i.e., "Your number is: " and then their number next to it?
Re: Random Number Generator?
Posted: Wed Nov 20, 2013 6:10 am
by micha
Op Winter Storm wrote:Is there a way that I can make the number go after a word, i.e., "Your number is: " and then their number next to it?
Replace the print-command by
Code: Select all
love.graphics.print('Your number is :' .. number, 10, 10)
Re: Random Number Generator?
Posted: Fri Nov 22, 2013 7:48 pm
by davisdude
By the way, you can also do other things with math.random:
With no arguments it returns a number between 0 and 1.
Returns an integer between 1 and 100 in this example.
Returns an integer between 1 and 100 in this example.
Re: Random Number Generator?
Posted: Mon Nov 25, 2013 7:49 am
by elsalvador
here might help had same problem with random hope it helps
Code: Select all
function love.load()
g=love.graphics
k={}
k.x=100
k.y=100
k.w=50
k.h=50
k.speed=200
u={}
u.x=400
u.y=100
u.w=50
u.h=50
Over_LAP=false
M =0
RR =0
TT = false
end
function love.draw()
g.setColor(255,255,255,255)
g.rectangle('line',k.x,k.y,k.w,k.h)
g.rectangle('line',u.x,u.y,u.w,u.h)
g.print('COLLISION: '..M,200,10)
g.print('RANDOM NUMBER: '..RR,200,30)
end
function love.update(dt)
if love.keyboard.isDown("d") then
k.x=k.x+k.speed*dt
end
if love.keyboard.isDown("a") then
k.x=k.x-k.speed*dt
end
--collision--
if (k.y + k.h > u.y ) and ( k.y < u.y + u.h ) and
(k.x + k.w > u.x ) and ( k.x < u.x + u.w ) then
Over_LAP=true
RR = math.random(1,10)
else
Over_LAP=false
end
if Over_LAP then
if not TT then
M = M + 1
TT = true
end
else
TT = false
end
end
Re: Random Number Generator?
Posted: Tue Nov 26, 2013 11:39 pm
by Op Winter Storm
Thanks! This all helps me a LOT! elsalvador, yours was a bit (by bit I mean a lot) over my head. Though it's still helpful, it's WAY over my head (like, miles and miles).