Random Number Generator?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Op Winter Storm
- Prole
- Posts: 5
- Joined: Fri Sep 06, 2013 7:05 pm
Random Number Generator?
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!
Basically, define a list of numbers (say, 1-100), and maybe some more advanced stuff later.
Thanks!
After eating an entire bull, a mountain lion felt so good he started roaring. He kept it up until a hunter came along and shot him. The moral: When you're full of bull, keep your mouth shut. - Will Rogers
Re: Random Number Generator?
Lua comes with the math.random() function. Have a look at http://www.lua.org/pil/18.html
Re: Random Number Generator?
Code: Select all
randomNumber = math.random(1, 100)
- Op Winter Storm
- Prole
- Posts: 5
- Joined: Fri Sep 06, 2013 7:05 pm
Re: Random Number Generator?
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?
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?
After eating an entire bull, a mountain lion felt so good he started roaring. He kept it up until a hunter came along and shot him. The moral: When you're full of bull, keep your mouth shut. - Will Rogers
Re: Random Number Generator?
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
- Op Winter Storm
- Prole
- Posts: 5
- Joined: Fri Sep 06, 2013 7:05 pm
Re: Random Number Generator?
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?
After eating an entire bull, a mountain lion felt so good he started roaring. He kept it up until a hunter came along and shot him. The moral: When you're full of bull, keep your mouth shut. - Will Rogers
Re: Random Number Generator?
Replace the print-command byOp 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?
Code: Select all
love.graphics.print('Your number is :' .. number, 10, 10)
Check out my blog on gamedev
Re: Random Number Generator?
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.
Code: Select all
math.random()
Code: Select all
math.random(100)
Code: Select all
math.random( 1, 100 )
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
-
- Citizen
- Posts: 54
- Joined: Thu Oct 24, 2013 1:29 am
Re: Random Number Generator?
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
- Op Winter Storm
- Prole
- Posts: 5
- Joined: Fri Sep 06, 2013 7:05 pm
Re: Random Number Generator?
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).
After eating an entire bull, a mountain lion felt so good he started roaring. He kept it up until a hunter came along and shot him. The moral: When you're full of bull, keep your mouth shut. - Will Rogers
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 4 guests