x


Random Numbers... Again

How can I generate two different random numbers in Unity at the same time without using time as the seed for the generator?

Thanks in advance - Elliot B.

more ▼

asked Jul 28 '10 at 03:07 AM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first
function Start () {
    var number1 = Random.value;
    var number2 = Random.value;
    Debug.Log (number1 + " " + number2);
}

The only reason to set the seed is if you want a repeating sequence of numbers.

more ▼

answered Jul 28 '10 at 03:18 AM

Eric5h5 gravatar image

Eric5h5
80.2k 41 132 519

Thanks, but won't number 1 and number 2 be the same?

Jul 28 '10 at 01:25 PM e.bonneville

@Elliot Bonneville, of course not, or I wouldn't have posted that. It takes about 2 seconds to try it, you know. ;) (Well...they might be the same by random chance, but it's pretty unlikely.)

Jul 28 '10 at 07:45 PM Eric5h5
(comments are locked)
10|3000 characters needed characters left

seeds will help you to generate different sequence of numbers. the random functions are not really random and they use an algorithm to do their job. seed is the number that is the input of the random function and will lead to different sequences.

don't change your seed when debugging but when you are sure that the code is working great. then use DateTime.now or ... to make it really random. so with one seed, each time when you call random you will get a different value but in multiple game sessions, you will get always those same random numbers. the sequence of numbers will be the same.

more ▼

answered Jul 28 '10 at 05:26 AM

Ashkan_gc gravatar image

Ashkan_gc
9.1k 33 56 117

You only need to set Random.seed if you specifically want a repeatable set of "random" numbers. Otherwise just leave it alone. Priming Random.seed with DateTime.Now is unnecessary, since the Random class already does that (or something like it) for you, as long as you don't touch Random.seed in the first place.

Jul 28 '10 at 05:47 AM Eric5h5
(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:

x572
x76

asked: Jul 28 '10 at 03:07 AM

Seen: 4073 times

Last Updated: Jul 28 '10 at 03:07 AM