• Create Account

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1jdub  Members

459
Like
0Likes
Like

Posted 02 March 2011 - 12:41 AM

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?
J.W.

### #2Hodgman  Moderators

49430
Like
0Likes
Like

Posted 02 March 2011 - 01:06 AM

Aren't x and y the seed value?

### #3LorenzoGatti  Members

4089
Like
2Likes
Like

Posted 02 March 2011 - 02:13 AM

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.

Omae Wa Mou Shindeiru

### #4haegarr  Members

7189
Like
1Likes
Like

Posted 02 March 2011 - 02:38 AM

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 : I2 -> [-1, 1]R
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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.