question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

numpy.random inconsistency of random numbers from other implementations

See original GitHub issue

Hi everyone,

I would like to point out something that could either be called a feature or bug. For a science analysis I want to use the deterministic nature of the Mersenne Twister algorithm in order to produce the same set of random numbers using different languages and libraries. I decided to try the Mersenne Twister generator MT19937ar. I test three libraries that (I think) implement this algorithm, the GNU Scientific Library gsl_rng_mt19937, gsl_rng_uniform and ROOT’s TRandom3 .Uniform() in C++, and numpy.random.RandomState, numpy.random.random_sample() with numpy. Given the same input seed, gsl and ROOT give the same outputs a[i]. However, numpy gives output b[i]=a[2i], in other words it produces every other number produced by the C++. It is as if the MT has two state changes in python for every one in gsl and ROOT.

Here is my python code

#!/usr/bin/env python

import numpy
import random
seed=1969
prng=numpy.random.RandomState(seed)
random.seed(seed)
for i in xrange(10):
  print prng.random_sample(), random.random()

and my C++ code

#include <iostream>
#include <gsl/gsl_rng.h>
#include "TRandom3.h"

int main(int argc, char* argv[])
{
unsigned long int seed=1969;
    gsl_rng* rng = gsl_rng_alloc( gsl_rng_mt19937 );
    gsl_rng_set(rng, seed);

    TRandom3 root(seed);

    for (int i=0; i<10; i++){
        std::cout << gsl_rng_uniform( rng ) << " "<<root.Uniform()<< std::endl;
    }
    gsl_rng_free(rng);
    return 0;
}

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pvcommented, Mar 24, 2014

FWIW, Numpy produces the same random number stream as matlab:

>> s = RandStream('mt19937ar','Seed',1969)
s = 
mt19937ar random stream
             Seed: 1969
  NormalTransform: Ziggurat
>> s.rand()
ans =
    0.5990
>> s.rand()
ans =
    0.5777
>> s.rand()
ans =
    0.9256
>> s.rand()
ans =
    0.1372

vs

>>> from numpy.random import seed, uniform
>>> seed(1969)
>>> uniform()
0.5989977136290761
>>> uniform()
0.5776863658432689
>>> uniform()
0.9256443316932297
>>> uniform()
0.13716928036018283
0reactions
charriscommented, Mar 23, 2014

Closing this. @AlexGKim You might check the outputs for random unsigned int32.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Numpy Random Numbers - inconsistent?
I am trying to generate log-normally distributed random numbers in python (for later MC simulation), and I find the results to be quite ......
Read more >
Random sampling (numpy.random) — NumPy v1.24 Manual
Numpy's random number routines produce pseudo random numbers using combinations of a BitGenerator to create sequences and a Generator to use those sequences...
Read more >
Generating Random Numbers and Arrays in Matlab and Numpy
In this post, we will discuss how to generate random numbers and arrays in both Matlab and Numpy. In particular, we make a...
Read more >
You've been using the Wrong Random Numbers! - YouTube
In this tutorial we discuss Monte Carlo convergence and the difference between Pseudo- random numbers and Quasi- random numbers.
Read more >
python - Generating t-distributed random numbers
Short version: the problem stands with NumPy x.std() which does not divide by the right degrees of freedom. ... for the next. ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found