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
97
votes
6 answers

How can I retrieve the current seed of NumPy's random number generator?

The following imports NumPy and sets the seed. import numpy as np np.random.seed(42) However, I'm not interested in setting the seed but more in reading it. random.get_state() does not seem to contain the seed. The documentation doesn't show an…
Mast
  • 1,788
  • 4
  • 29
  • 46
51
votes
3 answers

Does std::mt19937 require warmup?

I've read that many pseudo-random number generators require many samples in ordered to be "warmed up". Is that the case when using std::random_device to seed std::mt19937, or can we expect that it's ready after construction? The code in…
Brent
  • 4,153
  • 4
  • 30
  • 63
42
votes
5 answers

Robust Random Number Generation

I'm looking for a performant, reasonably robust RNG using no special hardware. It can use mathematical methods (Mersenne Twister, etc), it can "collect entropy" from the machine, whatever. On Linux/etc we have a drand48() which generates 48 random…
user2189331
39
votes
9 answers

How do I scale down numbers from rand()?

The following code outputs a random number each second: int main () { srand(time(NULL)); // Seeds number generator with execution time. while (true) { int rawRand = rand(); std::cout << rawRand << std::endl; …
Maxpm
  • 24,113
  • 33
  • 111
  • 170
31
votes
6 answers

Best way to seed mt19937_64 for Monte Carlo simulations

I'm working on a program that runs Monte Carlo simulation; specifically, I'm using a Metropolis algorithm. The program needs to generate possibly billions of "random" numbers. I know that the Mersenne twister is very popular for Monte Carlo…
Mathhead200
  • 331
  • 1
  • 3
  • 6
24
votes
8 answers

Random numbers for multiple threads

Problem I intend to write a C++11 application for Linux which does some numerical simulation (not cryptography) based on approximately one million pseudorandom 32bit numbers. To speed things up, I'd like to perform the simulation in parallel threads…
MvG
  • 57,380
  • 22
  • 148
  • 276
23
votes
1 answer

Clang performance drop for specific C++ random number generation

Using C++11's random module, I encountered an odd performance drop when using std::mt19937 (32 and 64bit versions) in combination with a uniform_real_distribution (float or double, doesn't matter). Compared to a g++ compile, it's more than an order…
Basti
  • 2,228
  • 1
  • 19
  • 25
18
votes
3 answers

Generate identical random numbers in R and Julia

I'd like to generate identical random numbers in R and Julia. Both languages appear to use the Mersenne-Twister library by default, however in Julia 1.0.0: julia> using Random julia> Random.seed!(3) julia> rand() 0.8116984049958615 Produces…
Colin T Bowers
  • 18,106
  • 8
  • 61
  • 89
15
votes
1 answer

How to properly seed a mersenne twister RNG?

This is actually not as simple as I first thought. In the absence of a hardware RNG, what is the best way to seed a Mersenne Twister? Or should I say, what is an acceptable way to seed a a Mersenne Twister RNG that is used to generate UUID's?
hookenz
  • 36,432
  • 45
  • 177
  • 286
15
votes
3 answers

Mersenne twister warm up vs. reproducibility

In my current C++11 project I need to perform M simulations. For each simulation m = 1, ..., M, I randomly generate a data set by using a std::mt19937 object, constructed as follows: std::mt19937 generator(m); DatasetFactory…
Ilio Catallo
  • 3,152
  • 2
  • 22
  • 40
15
votes
1 answer

How is PHP's mt_rand seeded?

I know PHP's mt_rand() should not be used for security purposes as its results are not cryptographically strong. Yet a lot of PHP code does just that, or uses it as a fallback if better sources of randomness are not available. So how bad is it?…
JanKanis
  • 6,346
  • 5
  • 38
  • 42
12
votes
9 answers

How reliable is the Random function in Delphi

I am writing a program which write statistical tests in Delphi (must be Delphi) and I've heard that the Random functionality is somewhat odd. You have to call randomize to randomize the seed of the random function when the program starts. I'm…
Daisetsu
  • 4,846
  • 11
  • 50
  • 70
12
votes
1 answer

Is there a C++11 CSPRNG?

As we know, the Mersenne Twister is not crytographically secure: Mersenne Twister is not cryptographically secure. (MT is based on a linear recursion. Any pseudorandom number sequence generated by a linear recursion is insecure, since from…
user5287986
  • 121
  • 1
  • 4
11
votes
2 answers

Is mersenne twister thread safe for cpp

#include int f() { std::random_device seeder; std::mt19937 engine(seeder()); std::uniform_int_distribution dist(1, 6); return dist(engine); } Can multiple threads call this function safely? Is the function thread…
cateof
  • 6,608
  • 25
  • 79
  • 153
11
votes
3 answers

Generating number (0,1) using mersenne twister c++

I'm working on implementing R code into C++ so that it runs faster, but I am having difficulties implementing mersenne twister. I only wish to generate values between (0,1). Here is what I have that pertains to this question. #include…
user3508622
  • 113
  • 1
  • 1
  • 4
1
2 3
10 11