x


How do I seed the random number generator myself and still get pseudorandom values?

How do I change the Random.seed myself relative to other variables to still get pseudorandom output?

Consider the following C# code:

   float RandomFromXAndY(int x, int y) {
        Random.seed = x * y; //Repeating, not pseudorandom.
        return Random.value;
    }

The problem is there are multiple combinations of x and y that can yield their same product (e.g., 3 * 6 or 6 * 3 both equal 18). So the result may yield the same, even when different combinations of x and y values are used. When generating x and y linearly, it becomes possible to mathematically predict the repetition.

How do I generate a random seed that is more "random", or pseudorandom from x and y?

more ▼

asked Jun 22 '12 at 09:20 PM

ColinBurke gravatar image

ColinBurke
40 2 4 7

yes you could also use the square root of both numbers.

Jan 19 at 09:08 AM ZoomDomain
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You add them together and multiply both by different prime numbers.

more ▼

answered Jun 22 '12 at 09:23 PM

whydoidoit gravatar image

whydoidoit
33.4k 14 23 104

This suggestion will effectively prevent visible repetition patterns/mirroring effects between x and y.

If you want to guarantee that there are no duplicate seeds whatsoever, at least one of your primes needs to be larger than your possible maximum value of x and y.

Jun 22 '12 at 10:51 PM Wolfram

@wolfram A damn good point.

Jun 22 '12 at 11:05 PM whydoidoit

Isn't the seed updated each time you use Random.XXX ? Calling Random.value twice doesn't get you the same thing. That doesn't each result unique though, using prime numbers is the way to go.

Jun 22 '12 at 11:11 PM Berenger

Hm, I haven't tried with Unity, but I thought the idea of a seed was that consecutive calls to Random.value produce the same series of random numbers for the same seed?

Jun 22 '12 at 11:18 PM Wolfram

Yeah that's what happens when I do it - I choose a seed then it repeats the same sequence, each step being pseudorandom. If you want to always get a random number between 0 and 1 (or whatever) and you want to make sure it is the same number given a set of standard inputs - it looks like prime numbers are the way to go.

Jun 22 '12 at 11:21 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left

You just need to be careful about trusting the random to be the same. If some other system calls random in the middle of this function, the same seed won't produce the same set of numbers since Random is global.

In reality, this system should be refactored so you make your own stream object, then you can control who is called it and insure that the stream of random number is always the same.

more ▼

answered Apr 30 at 11:46 PM

ceptri gravatar image

ceptri
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4359
x598
x222
x161
x10

asked: Jun 22 '12 at 09:20 PM

Seen: 2359 times

Last Updated: Apr 30 at 11:46 PM