math.random always generating the same number?

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
Eamonn
Party member
Posts: 550
Joined: Sat May 04, 2013 1:29 pm
Location: Ireland

math.random always generating the same number?

Post by Eamonn »

It isn't just me having this problem. My friend is having the exact same problem. So, I thought I'd ask the community what might be wrong.

The built-in Lua "math.random()" function usually works fine for me. My game "Mr. BallGuy" relies on it. That's how I randomly generate the balls. But now, for some reason, the following function always generates "0" for some reason. I've checked this using love.graphics.print to print the values of the X and Y position for the platform. Here it is:

Code: Select all

objects.platforms[objects.platforms.currentPlatform].x = math.random(width - objects.platforms[objects.platforms.currentPlatform].w)


objects.platforms[objects.platforms.currentPlatform].y = math.random(height - objects.platforms[objects.platforms.currentPlatform].h)
Me and my friend have both tried using "math.randomseed(os.time())", but it doesn't work. My friend isn't using LÖVE, he's making a command-line Random Name Generator, and he said he needs math.random for it. So, it's not a LÖVE problem.

Anyone got any ideas on what might be the issue?

Thanks!

File: https://www.dropbox.com/s/43edi26334sb9lr/Game.love
Last edited by Eamonn on Fri Aug 30, 2013 11:28 pm, edited 1 time in total.
"In those quiet moments, you come into my mind" - Liam Reilly
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: math.random always generating the same number?

Post by Plu »

What specific number is it generating? You are only supplying one argument which means it's generating a whole number between 1 and your upper bound.
User avatar
Eamonn
Party member
Posts: 550
Joined: Sat May 04, 2013 1:29 pm
Location: Ireland

Re: math.random always generating the same number?

Post by Eamonn »

It's generating 0 in both cases. I've tried providing it 0 and my other argument and it still produces 0 for some unknown reason.
"In those quiet moments, you come into my mind" - Liam Reilly
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: math.random always generating the same number?

Post by veethree »

And you're sure that this

Code: Select all

width - objects.platforms[objects.platforms.currentPlatform].w
Doesn't result in 0?


Also, You should know this by now, We can help you more if you provide a .love, Or at least some context :)
User avatar
Eamonn
Party member
Posts: 550
Joined: Sat May 04, 2013 1:29 pm
Location: Ireland

Re: math.random always generating the same number?

Post by Eamonn »

veethree wrote:And you're sure that this

Code: Select all

width - objects.platforms[objects.platforms.currentPlatform].w
Doesn't result in 0?

Also, You should know this by now, We can help you more if you provide a .love, Or at least some context :)
No, my problem is that it does result in 0.

I would have provided a .love, but I didn't think it was necessary because it was only those 2 pieces of code. Anyway, I'll update the post with a .love.
"In those quiet moments, you come into my mind" - Liam Reilly
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: math.random always generating the same number?

Post by bartbes »

He's not asking if the math.random returns 0, he's asking whether the argument to it returns 0.
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: math.random always generating the same number?

Post by veethree »

Yeah what bartbes said, If the argument is 0, math.random will be generating a random number between 0 and 0. Which will evidently be 0.
User avatar
DaedalusYoung
Party member
Posts: 407
Joined: Sun Jul 14, 2013 8:04 pm

Re: math.random always generating the same number?

Post by DaedalusYoung »

This is why I always implement a debug function in my projects. Print the values width and objects.platforms[objects.platforms.currentPlatform].w to screen. Then you can see if maybe there's an error somewhere else in your code, making them both the same number.
User avatar
Jeeper
Party member
Posts: 611
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: math.random always generating the same number?

Post by Jeeper »

DaedalusYoung wrote:This is why I always implement a debug function in my projects. Print the values width and objects.platforms[objects.platforms.currentPlatform].w to screen. Then you can see if maybe there's an error somewhere else in your code, making them both the same number.
Or print it to the console (since its made for that).
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: math.random always generating the same number?

Post by bartbes »

Also it doesn't have to be 0 per se, just less than one, because math.random returns integers when passed at least one argument.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 89 guests