Questions tagged [mersenne-twister]

The Mersenne Twister is a pseudo-random number generator (PRNG) suitable for Monte-Carlo simulations. It has a long period (2^19937-1) and yet takes very little memory space.

Mersenne Twister(MT) is a pseudorandom number generating algorithm developed by Makoto Matsumoto and Takuji Nishimura in 1996/1997. An improvement on initialization was given in 2002

It has a far longer period and far higher order of equidistribution than any other implemented generators. (It is proved that the period is 2^19937-1, and 623-dimensional equidistribution property is assured.)

Fast generation. (Although it depends on the system, it is reported that MT is sometimes faster than the standard ANSI-C library in a system with pipeline and cache memory.) (Note added in 2004/3: on 1998, usually MT was much faster than rand(), but the algorithm for rand() has been substituted, and now there is not much difference in speed on most platforms.)

Efficient use of the memory. (The implemented C-code mt19937.c consumes only 624 words of working area.)

Source code and documentation for the basic algorithm along with several variations can be found here: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html

165 questions
5
votes
2 answers

Thread safe mersenne twister

Looking for a thread safe random generator I found a mersenne twister generator class that the author says if thread safe: http://www.umiacs.umd.edu/~yangcj/mtrnd.html But after studying the code I cannot see were it is safe thread. There are no…
Horacio
  • 2,727
  • 5
  • 26
  • 29
5
votes
2 answers

How should I choose parameters for a smaller-than-standard std::mersenne_twister_engine?

I need a C++11 random number generator which is "good enough" and which I can save and restore state in. I want the saved state to be significantly smaller than the 6.6kb or so which this code produces std::mt19937 rng (1); std::ofstream save…
spraff
  • 32,570
  • 22
  • 121
  • 229
5
votes
2 answers

mt19937 and uniform_real_distribution

I am trying to find an efficient way to implement a uniform(0,1) distribution. Since I have to generate a very large number of samples, I chose mt19937 as engine. I am using the version from the boost library. My question is: what is the difference…
user3278488
  • 95
  • 1
  • 6
5
votes
2 answers

Is it safe to take only a few bits from a number obtained with a Mersenne Twister

I have to work with some code produced by an employee that is now retired, and I have some few strange things concerning random numbers. At some points, he shifted the value returned by a PRNG 10 bits to the right and then use a mask on this value.…
Loufylouf
  • 697
  • 5
  • 13
5
votes
2 answers

How do I use boost::random_device to generate a cryptographically secure 64 bit integer?

I would like to do something like this: boost::random_device rd; boost::random::mt19937_64 gen(rd()); boost::random::uniform_int_distribution dis; uint64_t value = dis(gen); But I've read that a mersenne twister is not…
Ben J
  • 1,367
  • 2
  • 15
  • 33
5
votes
1 answer

Comparing the Mersenne Twister in Java and matlab

I'm comparing the mersenne twister in Java and matlab. I'm using the same seed in both. My problem is that when i print out ten numbers from each number generator (Mersenne Twister running in Java and Matlab respectively), the resulting output…
4
votes
1 answer

Generate the same sequence of random numbers in C++ from a given seed

I am using mt19937 to generate a random string from a given seed like this: std::string StringUtils::randstring(size_t length, uint64_t seed) { static auto& chrs = "abcdefghijklmnopqrstuvwxyz"; thread_local static std::mt19937 rg(seed); …
jeffreyveon
  • 13,400
  • 18
  • 79
  • 129
4
votes
1 answer

Why do the numpy and random modules give different random numbers for the same seed?

For the same seed, why does random.random() produce different random values when compared to numpy.random(). My understanding is that they both use the Mersenne Twister to generate random values. import random as rnd import numpy as…
Aegir
  • 117
  • 11
4
votes
2 answers

Open-source implementation of Mersenne Twister in Python?

Is there any good open-source implementation of Mersenne Twister and other good random number generators in Python available? I would like to use in for teaching math and comp sci majors? I am also looking for the corresponding theoretical support.…
Tomaž Pisanski
  • 286
  • 4
  • 13
4
votes
1 answer

What algorithm does Math.random use?

Since I've been studying Computer Science, whenever random numbers come up, it's always Mersenne Twister. There's never even a question, no alternative. Just, use Mersenne Twister. So what does JavaScript's Math.random use? It seems like it ought to…
temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
3
votes
1 answer

Local variable in one function changing results in an unrelated function, C++

Just to forewarn folks, I'm a physicist writing a simulation which is probably toy code from a professional programmer's perspective, so my coding knowledge isn't great. Anyway, paraphrasing 700 lines, my code looks something like the…
3
votes
2 answers

Is Mersenne Twister a good binary RNG?

I'm trying to find an RNG to generate a stream of pseudorandom bits. I have found that Mersenne Twister (MT19937) is a widely used RNG that generates good 32-bit unsigned integers and that implementations have been done to generate apparently good…
Wilhelm
  • 1,868
  • 14
  • 21
3
votes
0 answers

R pseudo-random number generation change with same seed set with set.seed on 2 different machine

Issue: On R, running the function below leads to different seeds generation (and different set of random number) on 2 different machines but with the same starting seed with Mersenne-Twister algorithm: one on laptop on a docker container; one on an…
Florimond
  • 31
  • 2
3
votes
1 answer

boost::random generates identical values too often from the same seed at different states

Problem description Sometimes I get the same random number from a uniform distribution using a Mersenne Twister engine even I properly used the engine and iterated it. I know that the number of possible states of the engine is finite and number of…
DanielTuzes
  • 2,494
  • 24
  • 40
3
votes
2 answers

Different pseudo random numbers between C++ and Python implementations

I am trying to reproduce the C++ code into Python 3.6, but the sequence of pseudo random numbers is different in each implementation. The seed are the same on both implementation and as far as I know, both use Mersenne Twister algorithm. What am I…
Carlos Ost
  • 492
  • 7
  • 22
1 2
3
10 11