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
Help with 'math.random'
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- XxSolidJelloxX
- Prole
- Posts: 25
- Joined: Sun Jul 14, 2013 2:46 am
- Location: The Stars
Help with 'math.random'
- Attachments
-
- Num Test.love
- Here is the file:
- (976 Bytes) Downloaded 122 times
Re: Help with 'math.random'
You're using math.random, you probably were using love.math.random before.
- XxSolidJelloxX
- Prole
- Posts: 25
- Joined: Sun Jul 14, 2013 2:46 am
- Location: The Stars
Re: Help with 'math.random'
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.)foo0 wrote:You're using math.random, you probably were using love.math.random before.
- Zilarrezko
- Party member
- Posts: 345
- Joined: Mon Dec 10, 2012 5:50 am
- Location: Oregon
Re: Help with 'math.random'
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...
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 155 times
- XxSolidJelloxX
- Prole
- Posts: 25
- Joined: Sun Jul 14, 2013 2:46 am
- Location: The Stars
Re: Help with 'math.random'
Thank you very much! This will work perfectly for what I am doing. And yes, the Minecraft example helpedZilarrezko 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...
Re: Help with 'math.random'
At the start of each love game, this line is always run:
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.
Code: Select all
love.math.setRandomSeed(os.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.
Check out my blog on gamedev
Re: Help with 'math.random'
You generate a number from the default seed. Time has nothing to do with it.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...
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
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Help with 'math.random'
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.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Help with 'math.random'
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:
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:
When I write def I mean function.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Help with 'math.random'
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.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 2 guests