Two kinds of common algorithms for generating pseudo-random numbers

Source: Internet
Author: User
Tags generator rand
The random numbers we speak of actually imply pseudo random numbers. Many friends may think of the C language rand (), unfortunately, this function produces random number of randomness is very poor, and the speed is very slow, I believe almost not competent for general applications.

The ancient LCG (linear congruential generator) represents the best pseudo random number generator algorithm. The main reason is easy to understand, easy to achieve, and fast. This algorithm is mathematically based on a formula such as X (n+1) = (A * X (n) + C)% m, where:
Modulus m, M > 0
Coefficient A, 0 < a < m
Increment C, 0 <= C < m
Original value (Seed) 0 <= X (0) < M
The parameter C, M, a is more sensitive, or directly affects the quality produced by pseudorandom numbers.
In general, a high LCG m is an exponential power (generally 2^32 or 2^64) of 2, because the modulo operation truncates the 32 or 64 bits to the right. Most compiler libraries Use this theory to implement its pseudo random number generator rand (). The following are the individual parameter values used by some compilers:
Seed bit of Source m a C rand ()/Random (L)
Numerical Recipes
2^32 1664525 1013904223
Borland/C + +
2^32 22695477 1-digit 30. Rand (), 30..0 in Lrand ()
GLIBC (used by GCC)
2^32 1103515245 12,345-digit 30. 0
ANSI c:watcom, Digital Mars, CodeWarrior, IBM visualage/C + +
2^32 1103515245 12,345-digit 30. 16
Borland Delphi, Virtual Pascal
2^32 134775813 1-digit 63. Of (SEED * L)
Microsoft Visual/quick/C + +
2^32 214013 2,531,011-digit 30. 16
Apple CarbonLib
2^31-1 16807 0 See Park–miller random number generator

LCG cannot be used in situations where random numbers are required, such as not for Monte Carlo simulations and for cryptographic applications.
LCG has some serious flaws, for example, if LCG is used as the point coordinate of n-dimensional space, these points are at most on the m1/n hyperplane (Marsaglia theorem), which is caused by the Association of successive x (n) values produced.
Another problem is that if M is set to an exponent of 2, the lower sequence cycle is much smaller than the whole.
In general, the output sequence of cardinal B in the lowest n bits, bk = m (k is an integer), the maximum period bn.
Some occasions LCG have good applications, such as memory is very tight embedded in the video game console with small integers, the use of high level can be competent.
A C + + implementation version of LCG is as follows:
//************************************************************************
Copyright (C) 2008-2009 chipset
//
This are free software:you can redistribute it and/or modify
It under the terms of the GNU Affero general public License as
Published by the free Software Foundation, either version 3 of the
License, or (at your option) any later version.
//
but without any WARRANTY; Without even the implied warranty of
merchantability or FITNESS for A particular purpose. The
GNU Affero general public License for more details.
//
Should have received a copy of the GNU Affero general public License
Along with this program. If not, //************************************************************************
#ifndef LCRANDOM_HPP_
#define Lcrandom_hpp_
#include <ctime>

Class Lcrandom
{
Public
Explicit Lcrandom (size_t s = 0): seed (s)
{
if (0 = seed) seed = std::time (0);
Randomize ();
}

void Reset (size_t s)
{
Seed = s;
if (0 = seed) seed = std::time (0);
Randomize ();
}

size_t rand ()
{
Returns a random integer in the range [0, -1ul)
Randomize ();
return seed;
}

Double Real ()
{
Returns a random real number in the range [0.0, 1.0]
Randomize ();
Return (double) (seed)/ -1ul;
}

Private
size_t seed;
void Randomize () {seed = 1103515245UL * seed + 12345UL;}
};

Class Lcrand_help
{
Static Lcrandom R;
Public
Lcrand_help () {}
void operator () (size_t s) {r.reset (s);}
size_t operator () () const {return R.rand ();}
Double operator () (double) {return r.real ();}
};
Lcrandom lcrand_help:: R;

extern void Lcsrand (size_t s) {lcrand_help () (s);}
extern size_t Lcirand () {return lcrand_help () ();}
extern double Lcdrand () {return lcrand_help () (1.0);}

#endif//Lcrandom_hpp_

The Mersenne twister algorithm is a good choice if you need high quality pseudorandom numbers and sufficient memory (about 2KB). Mersenne Twister produces a random number of masses almost more than any LCG. However, the implementation of General Mersenne Twister uses LCG to produce seeds.
Mersenne Twister (Mason rotation algorithm) is a pseudo-random number generator developed by Makoto Matsumoto (Matsumoto) and Takuji Nishimura (West Village) in 1997, based on linear regeneration of matrices on finite binary fields. High quality pseudorandom numbers can be produced quickly, and many defects of the old random number generation algorithm are fixed. Mersenne Twister This name comes from the fact that the cycle length usually takes Mersenne prime numbers. Common for two variants Mersenne Twister MT19937 and Mersenne Twister mt19937-64.
Mersenne Twister has many advantages, such as: Cycle 2^19937-1 for general applications, large enough, sequence association is relatively small, can pass a lot of randomness test.
More detailed discussion about Mersenne twister please refer to http://www.cppblog.com/Chipset/archive/2009/01/19/72330.html
The pseudo random number version implemented with Mersenne twister algorithm is very much. For example, the high quality fast random number generator in boost library is written using the Mersenne twister algorithm principle.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.