There's more than one way to do things PerlMonks

### Re^2: Predictable random sequence

by vsespb (Chaplain)
 on Sep 17, 2013 at 14:42 UTC ( #1054457=note: print w/replies, xml ) Need Help??

in reply to Re: Predictable random sequence

Yep, this was also suggested by @Corion Re^3: Predictable random sequence, but seems it's not cleared in documentation that sequence is always same for same seed.

Replies are listed 'Best First'.
Re^3: Predictable random sequence
by BrowserUk (Pope) on Sep 17, 2013 at 14:49 UTC
but seems it's not cleared in documentation that sequence is always same for same seed.

It does, else it wouldn't be the Mersenne Twister.

Update: Results from a 32-bit and a 64-bit build of Perl (on windows):

```C:\test>\perl32\bin\perl -MMath::Random::MT=rand,srand -le"srand(1); p
+rint rand(12345) for 1 .. 50"
0.417021998437122 0.99718480813317 0.720324489288032 0.932557361200452
+ 0.000114381080493331
0.128124447772279 0.302332567749545 0.999040515394881 0.14675589255057
+3 0.236088976264
0.0923385955393314 0.39658072614111 0.186260211281478 0.38791074021719
+4 0.345560725079849
0.66974604036659 0.396767468657345 0.935539072612301 0.538816731888801
+ 0.846310918219388
0.419194519054145 0.313273512991145 0.685219500679523 0.52454816270619
+6 0.204452248988673
0.44345289375633 0.878117437008768 0.22957722004503 0.027387595968321
+0.534413907211274
0.670467507559806 0.913962022401392 0.417304804315791 0.45720480917953
+ 0.558689827332273
0.430698570096865 0.140386936953291 0.939127794932574 0.19810148328542
+7 0.778389235492796
0.800744566367939 0.715970510849729 0.968261571833864 0.80275750323198
+7 0.313424184685573
0.0928008102346212 0.692322616931051 0.518152548233047 0.8763891460839
+66 0.865020245313644

C:\test>\perl64\bin\perl -MMath::Random::MT=rand,srand -le"srand(1); p
+rint rand(12345) for 1 .. 50"
0.417021998437122 0.99718480813317 0.720324489288032 0.932557361200452
+ 0.000114381080493331
0.128124447772279 0.302332567749545 0.999040515394881 0.14675589255057
+3 0.236088976264
0.0923385955393314 0.39658072614111 0.186260211281478 0.38791074021719
+4 0.345560725079849
0.66974604036659 0.396767468657345 0.935539072612301 0.538816731888801
+ 0.846310918219388
0.419194519054145 0.313273512991145 0.685219500679523 0.52454816270619
+6 0.204452248988673
0.44345289375633 0.878117437008768 0.22957722004503 0.027387595968321
+0.534413907211274
0.670467507559806 0.913962022401392 0.417304804315791 0.45720480917953
+ 0.558689827332273
0.430698570096865 0.140386936953291 0.939127794932574 0.19810148328542
+7 0.778389235492796
0.800744566367939 0.715970510849729 0.968261571833864 0.80275750323198
+7 0.313424184685573
0.0928008102346212 0.692322616931051 0.518152548233047 0.8763891460839
+66 0.865020245313644

You should get the same everywhere regardless of platform or build.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
I am getting different results on linux:
```\$ perl -MMath::Random::MT=rand,srand -le"srand(1); print rand(12345) f
+or 1 .. 50"
5148.13657070627
12310.246456404
8892.40582026076
11512.4206240196
1.41203443869017
1581.69630774879
3732.29554886813
12333.1551625498
1811.70149353682
2914.51841197908
1139.91996193305
4895.789064212
2299.38230826985
4788.75808798126
4265.94715111074
8268.01486832555
4898.09440057492
11549.2298513988
6651.69255516725
10447.7082854183
5174.95633772342
3867.36151787569
8459.03473588871
6475.54706860799
2523.96301376517
5474.42597342189
10840.3597598732
2834.1307814559
338.099872228922
6597.33968452318
8276.92138082581
11282.8611665452
5151.62780927843
5644.19336932129
6897.02591841691
5316.9738478458
1733.07673668838
11593.5326284426
2445.5628111586
9609.21511215856
9885.19167181221
8838.65595643991
11953.189104289
9910.04137739888
3869.2215599434
1145.6260023464
8546.72270601382
6396.59320793697
10819.0240084066
10678.6749283969
Maybe you meant this?
```\$ perl -MMath::Random::MT=rand,srand -le"srand(1); print rand() for 1
+.. 50"
0.417021998437122
0.99718480813317
0.720324489288032
0.932557361200452
0.000114381080493331
0.128124447772279
0.302332567749545
0.999040515394881
0.146755892550573
0.236088976264
0.0923385955393314
0.39658072614111
0.186260211281478
0.387910740217194
0.345560725079849
0.66974604036659
0.396767468657345
0.935539072612301
0.538816731888801
0.846310918219388
0.419194519054145
0.313273512991145
0.685219500679523
0.524548162706196
0.204452248988673
0.44345289375633
0.878117437008768
0.22957722004503
0.027387595968321
0.534413907211274
0.670467507559806
0.913962022401392
0.417304804315791
0.45720480917953
0.558689827332273
0.430698570096865
0.140386936953291
0.939127794932574
0.198101483285427
0.778389235492796
0.800744566367939
0.715970510849729
0.968261571833864
0.802757503231987
0.313424184685573
0.0928008102346212
0.692322616931051
0.518152548233047
0.876389146083966
0.865020245313644
Anyway, even if numbers are same, point was it's not documented in module .pod, so might change in the future/in the past. Who knows.
UPD: seems second example same as yours.
Anyway, even if numbers are same, point was it's not documented in module .pod, so might change in the future/in the past.

If it does/did, it would not be a faithful implementation of the Mersenne twister.

UPD: seems second example same as yours.

It appears that the argument to rand is being ignored on both my windows installations but not on your linux install.

Which is intriguing as they should be using the same perl code. I don't have an explanation for that yet.

But, it you use it without an argument (ie. you retrieve just the 0-1 range random float and multiply it out yourself), you'll get the same results everywhere. That doesn't need to be documented because it is a fact of the design of the MT.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Hm. Intriguing. It seems that the Windows versions are doing the 'right thing'. Ie. ignoring the argument when you call rand and returning a random float 'tween 0 .. 1.

To get the equivalent behaviour of the built-in rand where the random float is multiplied by the argument, you need to import the function irand().

I cannot see by inspection why (or even how) you get different behaviour on linux?

It seems it should do the same thing as the Windows install:

```sub rand
{
my (\$self, \$N) = @_;

unless (ref \$self) {
Math::Random::MT::srand() unless defined \$gen;
\$self = \$gen;
}

return \$self->genrand();
}

sub irand
{
my (\$self, \$N) = @_;

unless (ref \$self) {
\$N = \$self;
Math::Random::MT::srand() unless defined \$gen;
\$self = \$gen;
}

return (\$N || 1) * \$self->genirand();
}

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
It does, else it wouldn't be the Mersenne Twister.
Which one?
here are at least two common variants of the algorithm, differing only in the size of the Mersenne primes used. The newer and more commonly used one is the Mersenne Twister MT19937, with 32-bit word length. There is also a variant with 64-bit word length, MT19937-64, which generates a different sequence.
Which one?

The one used by Math::Random::MT :) (Specifically, the version documented in the source code for the module.)

There are actually several variants of the MT, but the module only implements one of them, and it (the module) therefore produces the same sequences everywhere it compiles, regardless of whether it is compiled on a 32-bit or 64-bit platform; or what the underlying OS is. That is its main strength for simulation work.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Create A New User
Node Status?
node history
Node Type: note [id://1054457]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2017-08-19 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (312 votes). Check out past polls.

Notices?