Help with 'math.random'

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
XxSolidJelloxX
Prole
Posts: 25
Joined: Sun Jul 14, 2013 2:46 am
Location: The Stars

Help with 'math.random'

Post by XxSolidJelloxX »

Hey guys, I have looked for a solution and can't find one anywhere. Whenever I try to use math.random in love.load, it spits out the exact same number every time. I have used it in the past (never in love.load), and have never had this problem. Any help would be greatly appreciated.

Thanks :)
Attachments
Num Test.love
Here is the file:
(976 Bytes) Downloaded 97 times
foo0
Prole
Posts: 35
Joined: Sun Apr 27, 2014 10:25 am

Re: Help with 'math.random'

Post by foo0 »

You're using math.random, you probably were using love.math.random before.
User avatar
XxSolidJelloxX
Prole
Posts: 25
Joined: Sun Jul 14, 2013 2:46 am
Location: The Stars

Re: Help with 'math.random'

Post by XxSolidJelloxX »

foo0 wrote:You're using math.random, you probably were using love.math.random before.
Just replaced it math.random with love.math.random. It behaves differently, but still doesn't fix my problem (still spits out the same number each time the program is reloaded.)
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Help with 'math.random'

Post by Zilarrezko »

when using math.random() you generate a number relative to the time that the application starts. Or that's at least how I think of it... This will get ya the same number everytime...

What math.random uses is something called a seed to help actually make things appear random (even though there're still not... nothing is ever random.) but at least everytime you start your program you will get a different number!

add math.randomseed(os.time()) before the math.random in the love.load() this will give you what you want. As the OS's time always counts up, math.random will use that number as a reference to create something random.

To put it simply if you play minecraft... when you see the worldseed option. everytime (assuming they didn't change the algorithm in their random terrain) you enter in the same value be it a sequence of numbers or letters, you will get the same exact world everytime. This is what math.randomseed() does, yet instead of different terrain generation instead in this case you get a different number referenced by the seed.

Have Fun! ;)

P.S. here it is...
Attachments
Num Test (1).love
(1.07 KiB) Downloaded 101 times
User avatar
XxSolidJelloxX
Prole
Posts: 25
Joined: Sun Jul 14, 2013 2:46 am
Location: The Stars

Re: Help with 'math.random'

Post by XxSolidJelloxX »

Zilarrezko wrote:when using math.random() you generate a number relative to the time that the application starts. Or that's at least how I think of it... This will get ya the same number everytime...

What math.random uses is something called a seed to help actually make things appear random (even though there're still not... nothing is ever random.) but at least everytime you start your program you will get a different number!

add math.randomseed(os.time()) before the math.random in the love.load() this will give you what you want. As the OS's time always counts up, math.random will use that number as a reference to create something random.

To put it simply if you play minecraft... when you see the worldseed option. everytime (assuming they didn't change the algorithm in their random terrain) you enter in the same value be it a sequence of numbers or letters, you will get the same exact world everytime. This is what math.randomseed() does, yet instead of different terrain generation instead in this case you get a different number referenced by the seed.

Have Fun! ;)

P.S. here it is...
Thank you very much! This will work perfectly for what I am doing. And yes, the Minecraft example helped :)
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Help with 'math.random'

Post by micha »

At the start of each love game, this line is always run:

Code: Select all

love.math.setRandomSeed(os.time())
So if you use LÖVE's random number generator, you should have to do anything to get different behavior every time.

LÖVE's RNG has some advantages over the math.random function: First, it is the same an all machines. The math.random function is system dependent. And second, LÖVE's random function uses a better algorithm for generating the numbers. By "better" I mean, roughly speaking, that it is more difficult to see patterns in the numbers. For practical purposes in most games, however, the difference between the two algorithms is probably not relevant and you can use either.
foo0
Prole
Posts: 35
Joined: Sun Apr 27, 2014 10:25 am

Re: Help with 'math.random'

Post by foo0 »

Zilarrezko wrote:when using math.random() you generate a number relative to the time that the application starts. Or that's at least how I think of it... This will get ya the same number everytime...
You generate a number from the default seed. Time has nothing to do with it.

os.time() is not OS's time, but actual current time. More precisely it's usually a number of seconds passed since January 1 1970.

love.math.random seed is initialized with os.time() by default, so it will generate different numbers each run. It might spit the same numbers in a row occasionally thought, that's random.

// as os.time() resolution is one second, if you run your app repeatedly during one second, it will generate the same number every time. Give it a one second break before calling it busted ;)
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Help with 'math.random'

Post by bartbes »

However, in 0.9.1 (but not in 0.9.1) love also automatically throws away the first few numbers, because due to the way love.math's random number generator is implemented, if they start off with similar seeds (and I'm guessing you run your game in quick succession), their first few values are similar.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Help with 'math.random'

Post by kikito »

Just so you know, guys, I've also seen this happening.

The map I generate in Bad Guys Terrarium is based on math.random. I expected it to be always different, but it's the same every time I execute the program (zoom out with the mouse wheel to see it).

At least on my mac, it's always this one:
Screen Shot 2014-05-05 at 11.34.18 .png
Screen Shot 2014-05-05 at 11.34.18 .png (319.6 KiB) Viewed 3720 times
When I write def I mean function.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Help with 'math.random'

Post by bartbes »

Right, as said before, love stopped seeding math's random, and now seeds love.math's random. So yes, if you don't explicitly seed math's random then math.random will give you the same values. So either seed that, or use love.math.random.
Post Reply

Who is online

Users browsing this forum: Google [Bot], slime and 49 guests