## new guy with some noob qustions

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
zarko
Prole
Posts: 1
Joined: Wed May 16, 2012 12:25 am

### new guy with some noob qustions

I have a couple of questions about Loved and Lua. First i'm having a problem with trying to get random numbers using Lua. Using Scite or using the Lua online demo, the math.random function seems broken. For example if i write something like

Code: Select all

x=math.random(10)
print (x)
x will always be 9

I'm making a simple game with cannons and i want the cannon ball to travel in a arc, not a straight line. How is this done, do i need to use complex math?

nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

### Re: new guy with some noob qustions

math.random will return the same value the first couple of times you use it, for some reason. Try discarding a couple of random numbers after setting the seed:

Code: Select all

math.randomseed(os.time)
math.random()
math.random()
math.random()
As for the arc, that will require you to use a quadratic function.
richapple
Citizen
Posts: 65
Joined: Sun Dec 25, 2011 10:25 am

### Re: new guy with some noob qustions

zarko wrote:

Code: Select all

x=math.random(10)
print (x)
x will always be 9
That's because of included in Lua pseudo-random number generator (PRNG). The idea is very simple: the math.random returns a random number based on seed. For example default seed is 1. You execute a math.random(10) and it returns 9. Then you reopen the program and it will return the same number (basically what happened to you). Then if you seed it with math.randomseed() with for example 2 you will receive different number but this numer will be the same every time you restart your program. The best way is too seed the PRNG at the start of your program (LÖVE already does that, in love.run) Further reading: wikipedia

zarko wrote:I'm making a simple game with cannons and i want the cannon ball to travel in a arc, not a straight line. How is this done, do i need to use complex math?
I am sure that if you already implemented the straight moving of your cannon ball, you will just need to multiply the ball's y position every frame by an usuable amount.
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: new guy with some noob qustions

nevon wrote:math.random will return the same value the first couple of times you use it, for some reason. Try discarding a couple of random numbers after setting the seed:

Code: Select all

math.randomseed(os.time)
math.random()
math.random()
math.random()
As for the arc, that will require you to use a quadratic function.

I'm not 100% sure, but I think I saw somewhere that LÖVE 0.8 already called math.random several times after doing the ramdomseed. I might be wrong though.
When I write def I mean function.
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

### Re: new guy with some noob qustions

It calls it 2 times, it also calls math.randomseed(os.time()), in love.run
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
li = love.image
lg = love.graphics
10\$man
Citizen
Posts: 77
Joined: Sun Apr 22, 2012 10:40 pm