Jump to content

  • Log In with Google      Sign In   
  • 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.

  • You cannot reply to this topic
3 replies to this topic

#1 jdub   Members   -  Reputation: 419

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.

Sponsor:

#2 Hodgman   Moderators   -  Reputation: 31800

Like
0Likes
Like

Posted 02 March 2011 - 01:06 AM

Aren't x and y the seed value?

#3 LorenzoGatti   Crossbones+   -  Reputation: 2763

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.
Produci, consuma, crepa

#4 haegarr   Crossbones+   -  Reputation: 4586

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 Posted Image




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.



PARTNERS