[SciPy-user] How to generate equivalent "random" numbers in matlab and scipy?

Robert Kern robert.kern@gmail....
Sun Feb 22 22:07:54 CST 2009


On Sun, Feb 22, 2009 at 21:59, Nicolas Pinto <pinto@mit.edu> wrote:
> Dear all,
>
> I'd like to generate equivalent sequences of 'random' numbers in matlab and
> scipy, is there any way I can do that? I tried to fix the seed (see below)
> but it doesn't work.
>
> # scipy
> In [29]: random.seed(1); random.permutation(5)+1
> Out[29]: array([3, 2, 5, 1, 4])
>
> % matlab
>>> rand('seed', 1); randperm(5)
>
> ans =
>
>      4     3     5     2     1

A quick Google tells me that starting with Matlab 7.4, they do use the
Mersenne Twister by default, so it might be possible if you can
re-implement their seeding algorithm. Use RandomState.set_state() to
set the MT state vector directly (this is different from seeding,
which generates an MT state vector from a other kinds of inputs).

While this would give you identical sequences of samples (e.g.
random_sample(), randint(), etc.), there is no guarantee that the
algorithms we implement on top of the fundamental PRNG are the same
(e.g. permutation()).

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the SciPy-user mailing list