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
3
votes
1 answer

Converting to and from numpy's np.random.RandomState and Python's random.Random?

I would like to be able to convert back and forth between Python's standard Random and numpy's np.random.RandomState. Both of these use the Mersenne Twister algorithm, so it should be possible (unless they are using different versions of this…
Erotemic
  • 4,806
  • 4
  • 39
  • 80
3
votes
1 answer

Predict choice / bounded rand

the task i am given is to win 50 times in a row with a self-written client against this RockPaperScissor-PythonServer import SocketServer,threading,os,string import random, time f = open('secret.txt') offset = int(f.readline().strip()) choices = { …
3
votes
2 answers

Upper bound of random number generator

This is actually a follow up question of a previous one: Rounding of double precision to single precision: Forcing an upper bound After what I thought was the solution of my problems with the answer of previous question, I tried running my program…
3
votes
0 answers

C implementation of mersenne twister that produces results equal to mt_rand in PHP?

I'm working on some apps, and need to validate some results of random number generation. I can do it in PHP, the same as the source app, but it's slow. We'd like to code the validator in C for speed. The issue is that every implementation of the MT…
3
votes
2 answers

How do I save the state of std::mersenne_twister_engine to restore it later?

I would like to save the state of a std::mersenne_twister_engine so that I can restore it back exactly at a later time. I know I can save the original seed and call discard to roll the engine forward some number of steps, but that requires knowledge…
fbrereto
  • 35,429
  • 19
  • 126
  • 178
3
votes
3 answers

Does array_rand use the Mersenne Twister algorithm?

In PHP, we have the choice of mt_rand() and rand() where mt_rand() uses the Mersenne Twister algorithm and rand() uses the libc random generator. I would like to choose a random item out of an array using array_rand. However, does array_rand use…
F21
  • 32,163
  • 26
  • 99
  • 170
2
votes
2 answers

Generate random long long C++

int generator I currently generate deterministic pseudo-random ints using this code: #include #include #include #include const uint32_t CurrentTime =…
0liveradam8
  • 752
  • 4
  • 18
2
votes
0 answers

What kind of Mersenne Twister is being used in numpy.randomState?

Is numpy.random.RandomState using MT19937 or something else? All the document said was Mersenne Twister, and while I saw a few legacy ones uing MT19937, I never saw a straight answer. Thank you!
bombdruid
  • 53
  • 4
2
votes
2 answers

repeat random generation in parallel in c++, avoiding duplication over multiple runs

This is probably a really simple question but I'm pretty new to random number generation on c++ and want to make sure I'm getting it correct. I have a stochastic function that I want to run in parallel, multiple times, so each parallel run of the…
ALs
  • 509
  • 2
  • 4
  • 17
2
votes
1 answer

Visual Studio C++ 2012 & 2017 show different behaviour in random number generation?

I am sampling random numbers from normal distribution using the following code snippet under both Visual Studio 2012 (TC: 11.0) and Visual Studio 2017 (TC: 14.1): #include "stdafx.h" #include #include #include int…
2
votes
1 answer

Why am I getting numbers larger than 1000 when I %1000 a number generated by 64 bit mersenne twister engine?

I'm trying to generate a zobrist key for transposition tables in my chess engine. Here's how I'm generating the 64 bit numbers, as show here: How to generate 64 bit random numbers? typedef unsigned long long U64; std::random_device…
Max C
  • 369
  • 5
  • 16
2
votes
2 answers

Incorrectly seeding Mersenne Twister via constructor

What is wrong with my constructor? Every time I call a function (about once every five seconds) that is supposed to generate random numbers, it generates the same numbers. Each call instantiates one of these objects below. I thought I was seeding…
Taylor
  • 1,797
  • 4
  • 26
  • 51
2
votes
1 answer

Optimal constant seed for Mersenne Twister in C++ 11

My use case is: I need random numbers, but just for graphics (not for cryptography). I need to be able to get the same image (result) for 2 renderings/runs. For example, using time() as seed would not create the same result for the next run. So I…
MatthiasL
  • 81
  • 6
2
votes
1 answer

Proper copy of RNG in C++

I have a class containing a RNG: class Sampler{ private: std::random_device rd_; std::mt19937 gen_; public: Sampler(); double sample(); }; The class is initialized as follows: Sampler::Sampler() : …
beginneR
  • 3,207
  • 5
  • 30
  • 52
2
votes
1 answer

On the random number generation dSFMT performance on C++

I'm trying to find the most efficient way to generate random numbers for a MC simulation I'm working on. I've been reading a lot about the double precision Mersenne Twister algorithm and I wanted to understand a couple of basic things before moving…
epx
  • 799
  • 4
  • 12