7
\$\begingroup\$

I seem to recall Unity's Random is automatically seeded; checking the documentation it doesn't say it outright, but a certain interpretation of their words might seem to imply it.

The seed is normally set from some arbitrary value like the system clock before the random number functions are used. This prevents the same run of values from occurring each time a game is played and thus avoids predictable gameplay. However, it is sometimes useful to produce the same run of pseudo-random values on demand by setting the seed yourself.

(emphasis added)

\$\endgroup\$

3 Answers 3

6
\$\begingroup\$

While the text you've quoted may be interpreted in different ways, I'd consider the actual (Javascript) code example to be pretty obvious:

// If you comment out the following line, the sequence of random
// values will be different each time you run. However, the sequence
// will always be the same for any particular seed value.
Random.seed = 42;

This clearly states that you're able to not set any seed on your own and you'd still end up with "real" random numbers (i.e. the initial seed isn't an arbitrary number).

I don't think they'd make such a basic mistakes as providing platform dependent behavior (like forcing you to set a seed on a platform).

As such, I'd consider the wording normally set to refer to using some dynamic number (like time) rather than an arbitrary number. It's not about whether (or how) the seed is initialized by default.

\$\endgroup\$
4
\$\begingroup\$

I couldn't find any very reliable source but it seems that everybody agrees that the seed are automatically set to the current time when you create your random instance.

Someone here, here and here says that you should use random.seed only if you want repeatable sequences.

\$\endgroup\$
1
  • \$\begingroup\$ I've removed the reference to System.Random since it's quite OT, but the rest of the answer is good. \$\endgroup\$
    – o0'.
    Commented Jun 5, 2014 at 9:49
4
\$\begingroup\$

I made some tests. If you seed the random explicitly:

Random.seed = 50;
float r1 = Random.value;               // Same random sequence on each run
float r2 = Random.Range(0.0f, 100.0f); // Same random sequence on each run
int   r3 = Random.Range(0, 100);       // Same random sequence on each run 

If you don't seed it, then its totally random:

// Random.seed = 50; // Don't seed
float r1 = Random.value;               // Different random sequence on each run
float r2 = Random.Range(0.0f, 100.0f); // Different random sequence on each run
int   r3 = Random.Range(0, 100);       // Different random sequence on each run 
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .