Random Number Generator?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Op Winter Storm
Prole
Posts: 5
Joined: Fri Sep 06, 2013 7:05 pm

Random Number Generator?

Post 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! :awesome:
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
User avatar
Ensayia
Party member
Posts: 399
Joined: Sat Jun 12, 2010 7:57 pm

Re: Random Number Generator?

Post by Ensayia »

Lua comes with the math.random() function. Have a look at http://www.lua.org/pil/18.html
Teraku
Prole
Posts: 27
Joined: Mon Jun 24, 2013 10:01 am
Location: The Netherlands

Re: Random Number Generator?

Post 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.
User avatar
Op Winter Storm
Prole
Posts: 5
Joined: Fri Sep 06, 2013 7:05 pm

Re: Random Number Generator?

Post by Op Winter Storm »

Ok, thanks! :awesome:

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
Teraku
Prole
Posts: 27
Joined: Mon Jun 24, 2013 10:01 am
Location: The Netherlands

Re: Random Number Generator?

Post 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
User avatar
Op Winter Storm
Prole
Posts: 5
Joined: Fri Sep 06, 2013 7:05 pm

Re: Random Number Generator?

Post 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?
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
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Random Number Generator?

Post 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)
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Random Number Generator?

Post by davisdude »

By the way, you can also do other things with math.random:

Code: Select all

math.random()
With no arguments it returns a number between 0 and 1.

Code: Select all

math.random(100)
Returns an integer between 1 and 100 in this example.

Code: Select all

math.random( 1, 100 )
Returns an integer between 1 and 100 in this example.
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
elsalvador
Citizen
Posts: 54
Joined: Thu Oct 24, 2013 1:29 am

Re: Random Number Generator?

Post 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




User avatar
Op Winter Storm
Prole
Posts: 5
Joined: Fri Sep 06, 2013 7:05 pm

Re: Random Number Generator?

Post 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).
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
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 93 guests