Forgotten Password?

Or login with:

  • Facebook
  • Google
  • Yahoo
get GPL
this unit 1.98
sub units 0.00


viewed 2588 times and licensed 83 times
Random number generator class using the Mersenne Twister algorithm
Controller: CodeCogs



Class Mersenne

This class provides a very fast and very long period random number generator using Makoto Matsumoto's and Takuji Nishimura's Mersenne Twister algorithm. It produces uniformly distributed pseudo-random 32-bit values with period of


To give you an idea of the running time for each of the functions, here are the results for generating 100,000,000 random numbers on a 750MHz microprocessor :

This diagram is obtained by generating two sequences of 1000 uniform floating point random numbers in the (0, 1) interval and then plotting them one versus the other. It shows that the distribution is indeed uniform.


Example 1

The following example displays 20 random floating point numbers and 20 random large integers. It uses two different generators to achieve this. The first generator uses the system timer to initialize the seed, while the second is simply initialized with a particular value. Notice that it was necessary to divide the timer with the CARRYDIV value in order to keep the seed in the (0, 1) interval. The output of the first generator will obviously vary with each execution of the program, while the output of the second will always be the same if the seed is never changed.

#include <iostream>
#include <time.h>
#include <codecogs/statistics/random/mersenne.h>
using namespace std;
int main() {
    Stats::Random::Mersenne A(time(0) / MERSENNEDIV);
    Stats::Random::Mersenne B(0.638);
    for (int i = 0; i < 20; ++i)
        cout << A.genReal() << endl;
    cout << endl;
    for (int i = 0; i < 20; ++i)
        cout << B.genInt() << endl;
    return 0;


Lucian Bentea (August 2005)
Source Code

Source code is available when you agree to a GP Licence or buy a Commercial Licence.

Not a member, then Register with CodeCogs. Already a Member, then Login.

Members of Mersenne


Mersennedoubles = 0.46875 )[constructor]
sDefault value = 0.46875


voidsetSeeddoubles )
sSeed must be in the (0, 1) interval (endpoints are excluded).

Last Modified: 22 Dec 11 @ 00:00     Page Rendered: 2022-03-14 15:02:08