7
$\begingroup$

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.

$\endgroup$
10
  • 2
    $\begingroup$ 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]? $\endgroup$
    – MarcoB
    Commented Feb 7, 2016 at 5:15
  • 1
    $\begingroup$ Random uses an older and poorer PRNG algorithm. Let me see if I can find some references. $\endgroup$
    – Mr.Wizard
    Commented Feb 7, 2016 at 5:16
  • 2
    $\begingroup$ 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. $\endgroup$
    – MarcoB
    Commented Feb 7, 2016 at 6:23
  • 1
    $\begingroup$ At least somewhat related: (75303) $\endgroup$
    – Mr.Wizard
    Commented Feb 7, 2016 at 7:39
  • 3
    $\begingroup$ 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. $\endgroup$ Commented Feb 7, 2016 at 22:49

1 Answer 1

8
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – wolfies
    Commented Feb 7, 2016 at 14:34
  • $\begingroup$ @wolfies. Well, you should consider the default generators may be different. $\endgroup$
    – m_goldberg
    Commented Feb 7, 2016 at 14:47
  • 4
    $\begingroup$ +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. $\endgroup$
    – Mr.Wizard
    Commented Feb 8, 2016 at 3:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.