Math.randomseed lua problem.

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.
Post Reply
User avatar
Chartre
Prole
Posts: 4
Joined: Mon Jul 25, 2011 6:35 am

Math.randomseed lua problem.

Post by Chartre »

Hello, this is my first thread here. I have a problem with math.randomseed.

The problem is: math.random DOESN'T DO ANYTHING!
Well, to elaborate, I have a random seed for the x, y, and size of stars in a starfield. But when I try to generate the starfield, a small cluster of stars appear right in the corner of the screen, compared to the seemingly totally random array of stars generated when there is no seed. It doesn't seem to matter how ridiculous the seed is, it will always generate some sort of cluster of stars in the corner. I have tried to even generate the XYS locations for each star every time 10% of a loading sequence has finished, but to no avail.

Please help! If you find the ship controls Shonky Wonky Donkey, it's because I optimised the game for gamepad and don't really care about the keyboard controls. To generate 'new' stars, press F1. If you want to change the sort of control, press F2. Thanks!
Attachments
Spaceman.love
(425.42 KiB) Downloaded 1145 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Math.randomseed lua problem.

Post by Robin »

The thing about a seed is, you need to do it once. Then each call to math.random() will change the internal state of the random number generator.

So, you only need to call math.randomseed() once.
Help us help you: attach a .love.
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Math.randomseed lua problem.

Post by Boolsheet »

It looks like your understanding of the random functions is incomplete.

math.random returns a sequence of seemingly random numbers. math.randomseed sets the position where it will start in the sequence. Equal seeds will return the same random number sequence.
math.randomseed takes an integer from -2147483648 to 2147483647 (smaller and larger values work, but the number gets wrapped to this range anyway). The fraction gets rounded off.

Here's a simplified example how this could work in Lua:

Code: Select all

random_number = 1

function math.randomseed(seed)
	random_number = math.floor(seed)
end

function math.random()
	random_number = (1664525 * random_number + 1013904223) % 2147483648
	return random_number / 2147483648
end
This varies on different platforms, they are free to implement this how they want. Windows' crappy PRNG wraps with 32768 instead of 2147483648.

You either have to set the seed only once like Robin said, or create your own noise function.
Shallow indentations.
User avatar
Chartre
Prole
Posts: 4
Joined: Mon Jul 25, 2011 6:35 am

Re: Math.randomseed lua problem.

Post by Chartre »

Thanks! I suppose the seed was being called all the time, meaning the same number was being generated. It's now quite a pretty star-system generator.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Math.randomseed lua problem.

Post by Lafolie »

Nice Aphex reference.

Could post an updated .love file? I can't seem to get this one to do anything other than crash when I press return.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
Post Reply

Who is online

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