4
$\begingroup$

Let's say I need random numbers with increasing precision to simulate a particular function, so I try:

Table[SeedRandom[1]; 
  RandomReal[{0, 1}, WorkingPrecision -> i], {i, 1, 20}] // TableForm

enter image description here

While I was experimenting with this, I checked docs for Random and tried the following variation.

Table[SeedRandom[1]; Random[Real, {0, 1}, i], {i, 1, 20}] // TableForm

enter image description here


Question(s)

1- For my own learning, I was wondering if someone could please explain the difference.

2- How was something like this done prior to v6.0?

3- Or am I comparing apples with oranges?

4- With the same SeedRandom, why are different random numbers being generated for the 2nd case as the precision changes?

Thanks in advance for your help.

$\endgroup$
2
  • 2
    $\begingroup$ Probably a different algorithm is used for Random and RandomReal $\endgroup$ Jan 30, 2022 at 19:17
  • 3
    $\begingroup$ The change with precision between 19 and 20 is a hint that it switched from a 64 bit unsigned integer method to a different algorithm. Log[10, 2.^64] is 19.2659. $\endgroup$
    – John Doty
    Jan 30, 2022 at 19:24

2 Answers 2

4
$\begingroup$

I reproduce this behavior with versions 5.2, 8.0.4, 12.3.1, and 13.0.0 on Windows 10 x64:

screenshot

As you see, before version 6 the behavior of Random was exactly the same.

I guess that peculiarities like this were the main reason why Random was superseded by the functions RandomReal and RandomInteger in version 6.0. Apparently, Random is kept only for backward compatibility purposes. All usage examples for Random were removed from the Documentation in version 6. Here is how its page looked in Mathematica Help Browser of version 5.2:

screenshot

$\endgroup$
3
$\begingroup$

I. Earlier one might have controlled precision with SetPrecision.

Either generate a random number with higher precision than needed and lower the precision:

Table[SeedRandom[1]; 
 SetPrecision[Random[Real, {0, 1}, 100], i], {i, 1, 20}]

Or set the precision of a machine-precision number:

Table[SeedRandom[1]; 
 SetPrecision[Random[Real, {0, 1}], i], {i, 1, 20}]

II. The legacy algorithm of Random[] is available for RandomReal[] and kin via the Method option in SeedRandom.

SeedRandom[1, Method -> "Legacy"];
RandomReal[]

generates the same sequence of random reals as

SeedRandom[1, Method -> "Legacy"];
Random[Real]

This reproduces the OP's Random[Real...] table:

SeedRandom[Method -> "Legacy"];
Table[SeedRandom[1]; 
  RandomReal[{0, 1}, WorkingPrecision -> i], {i, 1, 20}] // TableForm
$\endgroup$

Your Answer

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

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