# Why do I keep getting the same random numbers?

I am trying to use Mersenne Twister pseudo random number generators as seeds for my Monte Carlo simulations. So I tried

BlockRandom[SeedRandom[1,Method->"MersenneTwister"];RandomReal[1,5]]


and it returns

{0.393562,0.701033,0.966231,0.221456,0.436768}


But evaluating again I still get the same numbers generated?
and not the same as if I did RandomReal[1,5] where each evaluation I get a new set of numbers? So my question is how to use MersenneTwister in the first line to generate different numbers each time it is evaluated?

-
Apparently, you didn't read the editing help page I showed you just yesterday... – rm -rf Nov 19 '12 at 4:17
Apparently, you didn't read the documentation for RandomSeed either. – m_goldberg Nov 19 '12 at 5:49
Apparently, I am going to pick some nit: it is SeedRandom. RandomSeed is obsolete. – István Zachar Nov 19 '12 at 8:42

You are resetting the random number generator with SeedRandom[n] every time with the same seed when you execute the block again. Hence you get the same sequence. SeedRandom is designed to recreate a repeatable random number sequence. Well pseudo random number sequence.

Set the seed and method once and then call RandomReal multiple times if you have too. Look up the documentation for SeedRandom under Application you will find an example that uses BlockRandom.

-

Re "So my question is how to use MersenneTwister in the first line to generate different numbers each time it is evaluated?" Use SeedRandom with the Method option but without the seed.

BlockRandom[SeedRandom[Method->"MersenneTwister"];RandomReal[1,5]]


Update

Re you question in comment "my question is if the numbers are random, then how to tell when I am using the Method-> "MersenneTwister" as opposed to not using it later on?": You can use the fact that they are really pseudo random, i.e. deterministic! See next function:

getRandomMethod :=
Module[{val, range = 2^8,
methods = {"Congruential", "ExtendedCA", "MersenneTwister", "MKL",
"Rule30CA"}},
SeedRandom[1];
val = RandomInteger[range];
BlockRandom[
Do[
SeedRandom[1, Method -> m];
If[RandomInteger[range] == val, Return[m]],
{m, methods}
]
]
];


which allows you to do something like:

In[9]:= SeedRandom[45, Method -> "MKL"]; getRandomMethod

Out[9]= "MKL"

Beware that getRandomMethod affects the current generator as it resets the seed. If you don't like this and want to keep track of the current method, I would keep it in a local variable.

Also, as witnessed by the following

Do[SeedRandom[1, Method -> m];
Print[{m, RandomInteger[256], RandomInteger[256],
RandomInteger[256]}],
{m, {"Congruential", "ExtendedCA", "Legacy", "MersenneTwister",
"MKL", "Rule30CA"}}];


{Congruential,58,71,123}

{ExtendedCA,134,31,228}

{Legacy,237,146,124}

{MersenneTwister,201,255,235}

{MKL,214,42,10}

{Rule30CA,237,146,124}

Rule30CA and Legacy are really the same. That's why I omit legacy in implementation above.

-
 So according to documentation -> Switch to the Mersenne twister generator: SeedRandom[Method -> "MersenneTwister"] and then Random numbers are now generated using it: RandomReal[1, 5], my question is if the numbers are random, then how to tell when I am using the Method-> "MersenneTwister" as opposed to not using it later on? – sebastian c. Nov 21 '12 at 22:03 I can't find anything in the documentation that exposes the current Method. The current state (kind of deprecated) is available via $RandomState but I believe it doesn't include the method. Anyone from Wolfram? If you want to isolate changes to your pseudo random generator to have reproducible simulations, use BlockRandom. – caya Nov 22 '12 at 18:35 By the way, I checked $RandomState and all are the same if you use the same seed, therefore the Method is not part of it. All, except "Legacy", which although seems to produce the same random numbers as Rule30CA, has a different \$RandomState... a curiosity. – caya Nov 23 '12 at 4:58