# Why is Random faster than RandomReal?

As of version 6, Random[] was superseded by RandomReal[ ] etc, but both still work perfectly well. Playing with some timing comparisons, I noticed that the original Random[] function appears to be faster, in single repeated drawings ...

ALSO, using the syntax RandomReal[] is faster than using RandomReal[{0,1}] ...

Table[RandomReal[{0,1}], {10^8}]; // AbsoluteTiming
Table[RandomReal[],      {10^8}]; // AbsoluteTiming
Table[Random[],          {10^8}]; // AbsoluteTiming


{10.1703,Null}

{7.00452, Null}

{4.99067, Null}

Similarly:

Do[RandomReal[{0, 1}], {10^7}]; // AbsoluteTiming
Do[RandomReal[],       {10^7}]; // AbsoluteTiming
Do[Random[],           {10^7}]; // AbsoluteTiming


{2.89869,Null}

{2.56348, Null}

{1.23561, Null}

Am I losing anything by not using the newer version, for basic Uniform(0,1) random draws like this?

EDIT: just to clarify ... the above refers to single calls on RandomReal / Random: in my application, I am not able to call $10^7$ drawings in advance.

• Typically it is more efficient to use a single call to RandomReal when generating multiple random numbers. How does the timing with Random compare with RandomReal[{0, 1}, 10^8]? – MarcoB Feb 7 '16 at 5:15
• Random uses an older and poorer PRNG algorithm. Let me see if I can find some references. – Mr.Wizard Feb 7 '16 at 5:16
• If relevant to your application, a description of the method used by Random (and of the new ones available to RandomReal) can be found in this tutorial: Random Number Generation under the "Legacy" entry. – MarcoB Feb 7 '16 at 6:23
• At least somewhat related: (75303) – Mr.Wizard Feb 7 '16 at 7:39
• As @ Mr.Wizard notes, Random is using a weaker pseudoRNG under the hood. It's a linear congruential generator due to Marsaglia and Zaman if I recall correctly. Very good in its day, and fast. But it does suffer on certain types of correlation and lattice tests. As for internet references, there may be notes on MathGroup about this. – Daniel Lichtblau Feb 7 '16 at 22:49

The following two expressions are equivalent.

Table[RandomReal[], {10^8}]; // AbsoluteTiming


{7.99593, Null}

RandomReal[1., 10^8]; // AbsoluteTiming


{1.20604, Null}

The second expression shows the advantage of RandomReal over Random.

### Edit

Another consideration is the generator used. For example, when the Mersenne twister is specified, there is not so much difference.

SeedRandom[1, Method -> "MersenneTwister"];
Table[RandomReal[], {10^8}]; // AbsoluteTiming


{8.16061, Null}

SeedRandom[1, Method -> "MersenneTwister"];
Table[Random[], {10^8}]; // AbsoluteTiming


{6.17236, Null}

It may be the default generators are different and are contributing to the discrepancy you have noticed.

• Unfortunately, this is not the issue raised. The question relates to repeated individual calls on RandomReal ... the application does not allow for generating them all in advance. – wolfies Feb 7 '16 at 14:34
• @wolfies. Well, you should consider the default generators may be different. – m_goldberg Feb 7 '16 at 14:47
• +1 for writing up the points made in the comments as a proper answer. A caveat however: Table compiles and is much faster than Do which is what @wolfies actually cares about (single draws). Also Method -> "Legacy" would probably be a better comparison. – Mr.Wizard Feb 8 '16 at 3:31