Generating pseudo-random numbers/sequences using linear congruential (C + + implementation)

Source: Internet
Author: User

Recently friends raised a question, their own writing function to generate random numbers, at first did not seriously think, and later thought, if it is learning computer cryptography, should be able to design a number of algorithms, the use of the relevant knowledge in the field of number theory-linear with congruential simple implementation of the generation of random numbers algorithm.

The following is a kind of statement on the internet about random number generation:

The computer can use physical methods to generate random numbers, but the price is expensive, can not be repeated, the use of inconvenience. The other method is to use the mathematical recursive formula to produce, so that the resulting sequence is different from the real random number sequence, so called pseudo-random number or pseudo-random sequence, as long as the method and parameter selection is appropriate, the resulting pseudo-random number can satisfy the uniformity and independence, and the real random number has similar properties.

The following is a recursive formula that uses linear congruence:
Xt = (X0 * +) mod 500

Linear congruence in linear, refers to the "linear" expression in the equation X number of times is a time, mod take the remainder operator embodies the "congruence" of the mathematical concept.

, 17, 29, and 500 are respectively called multipliers, increments, and modulus. The method of generating random numbers using linear congruence is fast, but it is necessary to select the multiplier, increment and modulus:

    1. The sequence produced by using the linear congruence formula multiple times should appear to be random and non-cyclic;
    2. Multiplier/increment and modulus coprime;
    3. This function can produce all random numbers in a full period. This requirement is controlled by a modulus.
#include <ctime>#include <iostream>classmyrand{ Public:unsigned intSeed//default to use system time as seed    //Time (NULL) returns the number of seconds from 0 o'clock midnight on 1970 New Year's day    voidSrandunsigned ints = (unsigned int) time (NULL)) {seed = s; }//using a linear and congruential, the maximum number of random numbers is (2^15-1), and 29 is one of the prime numbers    unsigned intRand () {seed = (seed * to+ -) % ((1<< the) -1);returnSeed }};
#include "rand.h"int main(){    MyRand a;    a.srand();  // 使用系统时间为种子    std::cout"产生若干个随机数:"std::endl;    for (int0100; i++)        std::cout100" ";  // 生成0~100之间的随机数    getchar();    return0;}

Using the wrong formula, the resulting sequence is not random:

Obtain a sequence of pseudo-random numbers that meet the requirements:

In the process of generating random numbers, it is emphasized that "pseudo" random numbers are generated, because the random numbers produced by invoking the rand () function (including the above-designed function) in programming are generated in accordance with a certain formula, and the result is deterministic and predictable. Fortunately, if the rand () function (including the above-designed function) is provided with different initial values (as random seed seeds), the true random number is the initial condition of the operation, and the real random number can be obtained.

is involved in the method of generating random seeds. There are many ways to produce seeds, and the most commonly introduced in the course of programming is to use system time as seed. The code uses time (NULL) to return the number of seconds from 0 o'clock midnight, 1970, to the present moment as the initial value of the random sequence operation, each time the rand () is called, a different random sequence is obtained.

In fact, this method of producing random seeds has some defects. Suppose you run a batch execution program on a single computer and the program executes several MS, then the seed of several neighboring programs is the same, and the result of each call to the random number generation function is the same. This is because the system time is calculated in seconds, and the execution time of the program is millisecond, and if multiple programs are executed within one second, the resulting random seed will inevitably be the same.

Reference Link: http://blog.sina.com.cn/s/blog_66edd39d0100l96s.html

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Generating pseudo-random numbers/sequences using linear congruential (C + + implementation)

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.