views:

124

answers:

5

For a game that I'm making, where solar systems have an x and y coordinates, I'd like to use the coordinates to randomly generate the features for that solar system. The easiest way to do this seems to seed a random number generator with two seeds, the x and y coordinates. Is there anyway to get one reliable seed from the two seeds, or is there a good PRNG that takes two seeds and produces long periods?

EDIT: I'm aware of binary operations between the two numbers, but I'm trying to find the method that will lead to the least number of collisions? Addition and multiplication will easily result in collisions. But what about XOR?

+5  A: 

Why not just combine the numbers in a meaningful way to generate your seed. For example, you could add them, which could be unique enough, or perhaps stack them using a little multiplication, for example:

seed = (x << 32) + y
tadman
+1  A: 
seed1 ^ seed2

(where ^ is the bitwise XOR operator)

chaos
A: 

why not use some kind of super simple fibonacci arithmetic or something like it to produce coordinates directly in base 10. Use the two starting numbers as the seeds. It won't produce random numbers suitable for monte carlo or anything like that, but they should be all right for a game. I'm not a programer or a mathmatician and have never tried to code anything so I couldn't do it for you.....

edit - something like f1 = some seed then f2 = some seed and G = (sqrt(5) + 1) / 2....

then some kind of loop. Xn = Xn-1 + Xn-2 mod(G) mod(1) (should produce a decimal between 0 and 1) and then multiply by what ever and take the least significant digits

and perhaps to prevent decay for as long as the numbers need to be produced...

an initial reseeding point at which f1 and f2 will be reseeded based on the generators own output, which will prevent the sequence of numbers being able to be described by a closed expression so...

if counter = initial reseeding point f1 = Xn and f2 = Xn - something. and... reseeding point is set to ceiling Xn * some multiplier.

so it's period should end when identical values for Xn and Xn - something are re-fed into f1 and f2, which shouldn't happen for at least what ever bit length you are using for the numbers.

.... I mean, that's my best guess...

A: 

Is there a reason you want to use the co-ordinates? For example, do you always want a system generated at the same coordinate to always be identical to any other system generated at that particular co-ordinate?

I would suggest using the more classical method of just seeding with the current time and using the results of that to continue generating your pseudo-randomness.

If you're adamant about using the coordinates, I would suggest concatenation (As I believe someone else suggested). At least then you're guaranteed to avoid collisions, assuming that you don't have two systems at the same co-ords.

Greg D
A: 

A simple Fibonacci PRNG uses 2 seeds One of which should be odd. This generator Uses a modulus which is a power of 10. The period is long and invariable being 1.5 times the modulus; thus for modulus 1000000 or 10^6 the period is 1,500,000. The simple pseudocode is:

Input "Enter power for 10^n modulus";m Mod& = 10 ^ m Input "Enter # of iterations"; n Input "Enter seed #1"; a Input "Enter seed #2"; b Loop = 1 For loop = 1 to n C = a + b If c > m then c = c - m A = b B = c Next

This generator is very fast and gives An excellent uniform distribution. Hope this helps.

Frank P. Smith