Pure linear homogeneous random number generator

Source: Internet
Author: User
Tags random seed
  1. Introduction to linear homogeneous random number generator:

The old LCG (linear congruential generator) represents the best and simplest pseudo-random number generator algorithm. The main reason is that it is easy to understand, easy to implement, and fast.

The LCG algorithm is based on the formula:

X (n + 1) = (A * x (n) + C) % m

The coefficients are as follows:

Module M, m> 0
Coefficient A, 0 <A <m
Incremental C, 0 <= C <m
Original Value (SEED) 0 <= x (0) <m
The parameters C, M, and a are sensitive, or directly affect the quality of pseudo-random numbers.
Generally, m with a high LCG value is the exponential power of 2 (generally 2 ^ 32 or 2 ^ 64), because the modulo operation truncates the rightmost 32 or 64 bit. This theory is used in most compiler libraries to implement its pseudo-random number generator rand ().

The following are the parameter values used by some compilers:

Source

M

A

C

Seeds of rand ()/random (l)

Numerical recipes

2 ^ 32

1664525

1013904223

 

Borland C/C ++

2 ^ 32

22695477

1

30 .. 16 in rand (), 30 .. 0 in lrand ()

Glibc (used by GCC)

2 ^ 32

1103515245

12345

30 .. 0

Ansi c: Watcom, digital Mars, codewarrior, IBM visualage C/C ++

2 ^ 32

1103515245

12345

30 .. 16

Borland Delphi, virtual Pascal

2 ^ 32

134775813

1

(63 .. 32 of (seed * l)

Microsoft Visual/quick C/C ++

2 ^ 32

214013

2531011

30 .. 16

Apple carbonlib

2 ^ 31-1

16807

0

See Park-Miller random number generator

LCG cannot be used for scenarios with high random numbers. For example, it cannot be used for Monte Carlo simulation or encryption applications.
LCG has some serious defects. For example, if LCG is used as the point coordinate of the n-dimensional space, these points are located at most on the M1/n superplane (marsaglia theorem ), this is because of the successive Association of x (n) values.
Another problem is that if M is set to 2, the generated low sequence cycle is much smaller than the overall.
In general, the minimum N bits in Base B of the output sequence, BK = m (K is an integer), and the maximum period bn.
In some cases, LCG has good applications, such as embedded systems with tight memory. Small integers used in the e-game console can be used with high positions.

(2) parsing of pseudo-random numbers in C: rand (), srand (Time (null)

In C language, the pseudo-random number generation algorithm actually uses the "linear same remainder method ". The specific calculation is as follows:

I = (Xi-1 * A + C) mod m

Where a, c, and m are constants (usually the prime number ). When C = 0, it is called the multiplication same method. The concept of seed is introduced. It will be substituted into the above formula as x0, and each time the rand () function is called, a random value will be generated to generate a new random value. It can be seen that the rand () function is actually used to generate a recursive sequence, and all values are derived from the original seed. So when the initial seed is the same, the obtained sequence is the same.

The C language contains a macro such as rand_max, which defines the range of random values that can be obtained by rand. In C, we can see that rand_max is defined as 0x7fff, that is, 32767. In the recursive formula of the Rand () function, the m value is 32767.

A pseudo-random number is generated by the linear coremainder method, which roughly conforms to the even distribution. According to the central limit theorem, any distributed noise can become Gaussian noise by repeating addition.

Function prototype

1. The C ++ standard function library provides a random number generator Rand, which returns a pseudo random integer evenly distributed between 0 and rand_max. Rand_max must be 32767 at least. The rand () function does not accept parameters. The default value is 1 (the start value ). The random number generator always starts with the same seed, so the pseudo-random sequence is also the same, without the random meaning. (But this facilitates program debugging)

2. In C ++, another function srand () can specify different numbers (unsigned integer yuan) as seeds. However, if the seeds are the same, the pseudo-random sequence is also the same. One way is to let the user input seeds, but it is still not ideal.

3. It is ideal to use a variable number, such as time, as the seed of the random number generator. The value of time varies every moment. So the seeds are different, so the random numbers are also different.

// C ++ random function (VC Program)

# Include <stdio. h>

# Include <iostream>

# Include <time. h>

Using namespace STD;

# Deprecision max 100

Int main (INT argc, char * argv [])

{

Srand (unsigned) Time (null); // The srand () function generates a Random Seed starting from the current time. It should be placed before loop statements such as for. Otherwise it will take a long time to wait.

For (INT I = 0; I <10; I ++)

Cout <rand () % max <Endl; // Max is the maximum value, and its random field is 0 ~ MAX-1

Return 0;

}

Rand () does not require a parameter. It returns an arbitrary integer from 0 to the maximum random number. The maximum random number is usually a fixed large integer. In this way, if you want to generate 0 ~ 10 integers, which can be expressed:

Int n = rand () % 11;

In this way, the value of N is a 0 ~ A random number of 10 ~ 10, it is like this:

Int n = 1 + rand () % 11;

In summary, it can be expressed:

A + rand () % N

Where a is the starting value and N is the range of integers.

A + rand () % (B-A + 1) indicates ~ A random number between B

0 ~ 1 decimal point, you can first get 0 ~ Then, divide all the values by 10 to get the 10 random decimal places from random to very. To get the random decimal places from random to percentile, you must first get 0 ~ The 10 Integers of 100 are all divided by 100. In other cases, this is the case.

Generally, the random numbers generated by rand () are the same as the previous one during each operation. This design is intended to facilitate program debugging. To generate different random numbers each time, you can use the srand (SEED) function for randomization. Different random numbers can be generated as the seed varies.

As you said, it can also contain time. h header file, and then use srand (time (0) to use the current time to randomize the random number generator, in this way, different random number sequences can be obtained every two runs (as long as the interval between two runs exceeds 1 second ).

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.