Generate pseudo-random number Xiang solution in VC ++

Source: Internet
Author: User
Tags random seed

Abstract pseudo-random numbers are widely used in computer software design. This article introduces a mathematical method to use computers to generate pseudo-random numbers, that is, the linear same-remainder method. Any pseudo-random numbers are generated using recursive principles. And the two important functions that generate pseudo-random numbers in the Visual C ++ environment, Rand and srand. correct use of these two functions is the key to producing good pseudo-random numbers, finally, we introduce the use of pseudo-random number generation technology to generate a Random verification code for C/S-based applications in MFC and the use of a software tool imagepassword to generate a random password.

Key words: pseudo-random number generation; linear same-remainder method; Visual C ++; random check code

In pursuit of real random sequences, people have used many primitive physical methods to generate a uniform distribution sequence that satisfies the precision (number of digits) within a certain range. Its disadvantages are: it is slow, inefficient, occupies a large amount of storage space, and cannot be reproduced. To meet the needs of Computer Simulation Research, people turn to research and use algorithms to generate pseudo-random sequences that simulate various probability distributions. A pseudo-random number is a random number generated by a mathematical recursive formula. From a practical point of view, the simplest and most natural way to obtain such numbers is to use the random number generator provided by the function library of computer languages. In typical cases, It outputs a pseudo-random variable value evenly distributed in the range of 0 and 1. One of the most widely used and thoroughly studied algorithms is the linear same-remainder method.
Www_bitscn_com

Linear congruence Generator)

Select a positive integer m that is large enough and any natural number N0, A, B, by recursive formula:

Ni + 1 = (AF (NI) + B) mod m I = 0, 1 ,..., M-1

The generated numerical sequence is called coordinalsequence. When function f (n) is a linear function, a linear cool sequence is obtained:

Ni + 1 = (A * Ni + B) mod m I = 0, 1 ,..., M-1

The following is the pseudo code used to generate pseudo-random numbers using the linear same-remainder method:

Random (n, m, seed, a, B)
{
R0 = seed;
For (I = 1; I <= N; I ++)
Ri = (A * ri-1 + B) mod m
}

The seed parameter can be selected as needed. It is often set to the current date or time of the computer. M is a large number, which can be set to 2 W, and W is the word length of the computer; A can be any integer between 0.01w and 0.99w.

When the recursive formula is used to generate a random number with a uniform distribution, the selection of the N0, A, B, and M parameters in the formula is very important. Network management has bitscn.net

For example, if M = 10, a = B = N0 = 7 is selected, the generated random sequence is {6, 9 ,......}, The cycle is 4.

Take m = 16, A = 5, B = 3, N0 = 7, the generated random sequence is {6, 1, 6, 1 ......}, The period is 16.

Take m = 8, A = 5, B = 1, N0 = 1, the generated random sequence is {6, 7, 4, 5, 2, 3, 1, 6, 7 ......}, The period is 8.

Pseudo-Random Number Generation Mechanism in Visual C ++

Using VC to generate random numbers has two functions: rand (void) and srand (SEED ). The random integer generated by rand () is between 0 and ~ Rand_max is a constant (defined as # define rand_max 0x7fff ). It is the maximum value of short data. If you want to generate a floating-point random number, you can convert rand ()/1000.0 To Get A 0 ~ A random floating point number that is evenly distributed between 32.767. If you want to increase the range, you can generate a linear combination of several random numbers to realize random numbers distributed evenly in any range.

The usage is to call the srand function first, as shown in figure

Srand (unsigned) Time (null ))

In this way, the random number sequence generated each time is different. If the initial values (called seeds) of the pseudo-random sequence are the same, the calculated pseudo-random sequence is exactly the same. To solve this problem, you must specify different seeds before each random sequence is generated, so that the calculated random sequence will not be identical. The time function value (that is, the current time) is used as the seed number, because the time for calling the rand function twice is usually different, which ensures randomness. You can also use the srand function to specify the number of seeds. Network Management ujia www.bitscn.net
Analyze the following two sections,

Procedure 1:

// Contains the header file
Void main (){
Int COUNT = 0;
For (INT I = 0; I <10; I ++ ){
Srand (unsigned) Time (null ));
Count ++;
Cout <"no" <count <"=" <rand () <"";
If (! (Count % 5) cout <Endl;
}
}

Procedure 2:

// Contains the header file
Void main (){
Int COUNT = 0;
Srand (unsigned) Time (null ));
For (INT I = 0; I <10; I ++ ){
Count ++;
Cout <"no" <count <"=" <rand () <"";
If (! (Count % 5) cout <Endl;
}
}

The running result of section 1 is:

No1 = 9694 NO2 = 9694 NO3 = 9694 No4 = 9694 no5 = 9694
China Network Management Forum bbs.bitscn.com

No6 = 9694 no7 = 9694 no8 = 9694 no9 = 9694 No10 = 9694

The running result of section 2 is:

No1 = 10351 NO2 = 444 NO3 = 11351 No4 = 3074 no5 = 21497
No6 = 30426 no7 = 6246 no8 = 24614 no9 = 22089 No10 = 21498

It can be found that the running results of the above two program segments vary according to the seed selected during random number generation. The rand () function returns the next number in the random number sequence (in fact, it is a pseudo-random number sequence, and each number in the sequence is obtained by a complex transformation of the previous number ). To simulate real randomness, you must first call the srand () function to set a seed for the sequence. In order to better satisfy the randomness, the time function time () is used to obtain a value that changes with time, so that each time the rand () function is run from srand () the seed values obtained by the function are different. The pseudo-random number generator transmits the number of "seeds" as an initial integer to the function. This seed will keep the ball rolling (generating pseudo-random numbers.

In section 1, the srand () function is placed in the loop body, and the CPU execution time of the program is fast, but the time precision obtained by calling the time function is low (55 ms ), in this way, the number of seeds used for each random number generation in the loop body is the same, so the random number is also the same. The random number generated for 1st times in Section 2 must use the Random Seed. In the future, the random number will be generated using the recursive relationship. [Transferred from www.bitscn.com]

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.