# Have the Random functions changed?

I re-ran a program I wrote for Mathematica v8, now on v9, and it appears to generate different RandomInteger numbers, even though I use the same SeedRandom at the beginning.
Have Random functions changed in v9?

-
To give a particular example, what does BlockRandom[SeedRandom[42, Method -> "ExtendedCA"]; RandomInteger[{-9, 9}, 10]] return on version 9? – J. M. Jun 22 '13 at 7:54
@0x4A4D - {4, -5, -7, -8, -3, -9, -5, -6, -1, 3} – stevenvh Jun 22 '13 at 7:57
hmmm. both v8 and v9 produce the same sequence for me... – Stefan Jun 22 '13 at 7:58
You might find ArrayReshape better fitting your needs. There is an adequate example in its documentation. – Artes Dec 4 '13 at 17:29
@Artes - Thanks, I'll look at it. Any reason why it's better than Partition (which works now)? – stevenvh Dec 4 '13 at 17:44

The default random number generation method has changed from version 8 to version 9.

The default methods for version 8 and version 9 are:

PRNG                |     v8           |  v9
-----------------------------------------------------
RandomInteger[]     |    ExtendedCA    |  Rule30CA
RandomReal[]        |    ExtendedCA    |  Rule30CA


The following does produce the same results for both versions:

methods = {
"Congruential",
"ExtendedCA",
"Legacy",
"MersenneTwister",
"MKL",
"ParallelGenerator",
"ParallelMersenneTwister",
"Rule30CA"
};

Table[BlockRandom[SeedRandom[42, Method -> m];
Print[m <> ": ", RandomInteger[{-9, 9}, 10]];
RandomInteger[{-9, 9}, 10]], {m, methods}];

Congruential: {-9, 0, 3, -7, -9, -3, 5, -1, 7, 4}

ExtendedCA: {4, -5, -7, -8, -3, -9, -5, -6, -1, 3}

Legacy: {9, -9, 0, 5, 7, -5, 9, 8, -1, 0}

MersenneTwister: {7, 6, 5, 2, 0, -3, 4, 4, -1, 8}

MKL: {-5, 2, -8, -3, -3, -5, 1, 1, 3, -5}

ParallelGenerator: {-2, 2, 3, 0, -6, -7, 3, -7, -9, -1}

ParallelMersenneTwister: {-4, -4, -1, -2, 0, -9, 0, -8, -9, -8}

Rule30CA: {9, -9, 0, 5, 7, -5, 9, 8, -1, 0}


If you use the default method, you indeed get different results:

BlockRandom[SeedRandom[42]; RandomInteger[{-9, 9}, 10]]


v8:

{4, -5, -7, -8, -3, -9, -5, -6, -1, 3}


v9:

{9, -9, 0, 5, 7, -5, 9, 8, -1, 0}


If they claim that the default is "ExtendedCA", than this is not true for version 9. For version 9, the default is "Rule30CA", while on version 8 it uses "ExtendedCA".

So if you want to produce the same random numbers on version 9, you have to explicitly set the method to "ExtendedCA".

Incidentally, the reason why "Legacy" and "Rule30CA" gave the same results for RandomInteger is that the legacy method does use the rule 30 generator in the integer case.

Now, let's test if this holds for RandomReal as well:

Table[BlockRandom[SeedRandom[42, Method -> m];
Print[m <> ": ", InputForm @ RandomReal[10, 10]]], {m, methods}];

Congruential: {3.8461335440024715, 3.354304567149635, 4.51935934053208,
8.850230228014308, 3.6248509154618356, 8.157875428939777,
2.4513895134473387, 5.818949583945838, 4.969467831268059,
0.03347488955145989}

ExtendedCA:   {4.259052837159626, 3.910231598741685, 3.4706935350965935,
4.53740630685645, 5.5596334203812, 2.8916925336738952,
2.9684806264504378, 2.064076444638916, 3.251697057830391,
9.733246886114031}

Legacy:       {8.9572216046609, 4.970307857530802, 5.435846534404246,
7.085955661473399, 6.776487260106322, 8.054379667584591,
0.7094411741824413, 8.968322445752152, 8.901306909526046,
0.7502088503156987}

MersenneTwister: {5.153950855950999, 0.6727671698455833, 4.524316138874237,
9.921991209186388, 0.5443991248015738, 8.437734478961886,
6.959208913527714, 9.091869760437504, 2.310061068071903,
8.976972757532419}

MKL:          {2.2010771720728672, 5.882083584870089, 0.7266383901861087,
3.3726500591458435, 3.3079876451216217, 2.488652193017376,
5.6151582821198005, 5.305289374386305, 6.735427495488978,
2.29022838609706}

ParallelGenerator: {2.8013742426114714, 8.147026088771263, 3.8405810263728526,
2.5308472947817595, 9.400530227458631, 9.054157508803371,
9.449607002369156, 4.3141703917759315, 6.087645635734276,
6.8637216165050745}

ParallelMersenneTwister: {9.999076480701376, 9.830697737962758, 8.382728461259866,
9.292582323124666, 0.487553230365652, 5.914364713788565,
1.6141971841606058, 1.4211558070777137, 9.501590480279376,
2.6447011005402707}

Rule30CA:     {5.633636625848604, 0.19140585740303528, 6.149388826993686,
1.9144953965106026, 2.9253076004819345, 0.55162786941208,
1.995816904778028, 9.427477798414383, 0.39607049447435116,
1.8220850707267502}


Both lists are identical between version 8 and version 9.

Let's use the default method for RandomReal:

v8:

BlockRandom[SeedRandom[42]; InputForm @ RandomReal[10, 10]]

{4.259052837159626, 3.910231598741685, 3.4706935350965935,
4.53740630685645, 5.5596334203812, 2.8916925336738952,
2.9684806264504378, 2.064076444638916, 3.251697057830391,
9.733246886114031}


So the default method for version 8 is "ExtendedCA".

v9:

BlockRandom[SeedRandom[42]; InputForm @ RandomReal[10, 10]]

{5.633636625848604, 0.19140585740303528, 6.149388826993686,
1.9144953965106026, 2.9253076004819345, 0.55162786941208,
1.995816904778028, 9.427477798414383, 0.39607049447435116,
1.8220850707267502}


And the default method for version 9 is "Rule30CA".

-
Huh, that's a step back. Here I thought they replaced the legacy generator with the newer CA-based ones precisely because the legacy method had not too good statistical properties... – J. M. Jun 22 '13 at 8:39
I've hade the same 'yikes!' moment...in chess something like this would've been called a blunder. – Stefan Jun 22 '13 at 8:50
Well, for completeness' sake, could you also do tests replacing RandomInteger[{-9, 9}, 10] with RandomReal[1, 10]? Maybe use InputForm[] so that all the digits of the random variates are displayed... BTW, to cover the default method in the results of the Table[], you can use the setting Method -> Automatic. – J. M. Jun 22 '13 at 9:19
@0x4A4D done. if you want to have a look at it and feel free to improve things. btw. i've used coloured Grid's in my notebook, but it seems to be impossible to paste this here in SE, that's why i've used Table. – Stefan Jun 22 '13 at 10:03
Actually, digging deeper into old docs, it would seem that the legacy method in fact uses the rule 30 cellular automaton in the integer case. This is borne out by the fact that "Legacy" and "Rule30CA" give identical results for RandomInteger[]. In short, version 8 uses "ExtendedCA" as the default, while version 9 uses "Rule30CA" by default, for both random integers and reals. Now we wait for somebody from WRI to explain this switch... – J. M. Jun 22 '13 at 10:12