• Advertisement
Sign in to follow this  

How can I modify this Pseudo Random Number Generator to use a Seed?

This topic is 2517 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So... I have this pseudo random number generator which takes 2 integers and outputs a unique pseudo random number (-1 to 1) for every pair:



private double noise(Int32 x, Int32 y)
{
Int32 n = x + y * 57;
n = ((Int32)n << (Int32)13) ^ (Int32)n;
n = (n * (n * n * 15731 + 789221) + 1376312589);
n = n & 0x7fffffff;
double nDouble = (double)n / 1073741824.0d;
return (1.0d - nDouble);
}



How would I modify this function so that I can have it accept a seed value?

Share this post


Link to post
Share on other sites
Advertisement
This function is not a PRNG, it is a hash function that outputs one double from two ints; it isn't relevant.
A PRNG has a permanent internal state and after being seeded once it can produce a sequence of pseudorandom numbers, until it repeats itself.
If you are working in C#, there's a .NET framework class named Random that can be initialized with a Int32 and has a NextDouble method.

Share this post


Link to post
Share on other sites

Aren't x and y the seed value?

Not in the sense of the OP, because the function isn't a pseudo random number generator at all. It is just a function f : I[sup]2[/sup] -> [-1, 1][sub]R[/sub]
For every pair (x, y) put in you'll get a deterministic result independent on the history of requests so far.

In CG one often wants PRN generators that generate reproducible number sequences, just to ensure that e.g. an appearance doesn't alter randomly (;)) from frame to frame. So the question is: The function is thought for ... what purpose? Why feeding in 2 parameters where a single one would have been equally good (e.g. noise( x + y * 57 ) if one wants to reduce the dimensionality).

To answer the OP in a general way: Make 2 function, the 1st to set the seed and the 2nd to request the next PRN. Let the seed function set a private variable, and let the request function base on that private variable and alter it. E.g.


private Int32 n;

public void setSeed(Int32 x, Int32 y) {
n = x + y * 57;
}

public double getNoise() {
// altering n
n = ((Int32)n << (Int32)13) ^ (Int32)n;
n = (n * (n * n * 15731 + 789221) + 1376312589);
n = n & 0x7fffffff;

// transforming into result range
double nDouble = (double)n / 1073741824.0d;
return (1.0d - nDouble);
}

(w/o saying that this is a good generator, and ignoring that x and y may have a special purpose). The point is that the n is stored, so that a sequence of requests can generate different results for each invocation.


EDIT: Either I'm so slow in writing or LorenzoGatti's post was deferred by the internet unsure.gif

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement