The setting of random number in C + +

Source: Internet
Author: User
Tags generator rand

Random and Rando can produce random numbers, contained in Stdlib.h.

The random function is not an ANSI C standard and cannot be compiled with a compiler such as GCC,VC. But in C, int random (num) can be used in this way, which returns a random number from 0 to num-1. You can use the RAND function under C + + instead.

1. C + + Standard function library provides a random number generator RAND, which returns a pseudo-random integer evenly distributed between 0-rand_max. The Rand_max must be at least 32767. The rand () function does not accept arguments, and the default is 1 seed (that is, the starting value). Random number generators always start with the same seed, so the pseudo-random sequence is the same, and the random meaning is lost. (but this is convenient for program debugging)

2, another function in C + + Srand (), you can specify a different number (unsigned integer variable) as a seed. But if the seeds are the same, the pseudo-random sequence is the same. One way is to let the user input the seed, but still not ideal.

3, the more ideal is to use the number of changes, such as time as a random number generator seed. The value of time is different every moment. So the seeds are different, so the random numbers that are produced are different.

Rand uses questions and answers:

Q: Why does rand () sometimes use it all the same number? How do I generate a real random number?

A: Rand () has the same random number each time because the rand () function is not used correctly. The number of random numbers returned by various programming languages (or, exactly, pseudo-random numbers) is actually a set of values calculated according to the recursive formula, and when the sequence is long enough, the set of values is approximately equal to the uniform distribution. If the initial value of the pseudo-random sequence (called the Seed) is the same, the computed pseudo-random sequence is exactly the same. This feature is used by some software for encryption and decryption. When encrypting, a pseudo-random sequence can be generated with a certain seed number and processed, and then the seed number is used to generate a pseudo-random sequence and the encrypted data is restored. In this way, for those who do not know the number of seeds to decrypt the need for more things. Of course, this exact same sequence is very bad for you. To solve this problem, we need to specify different seeds before each random sequence is generated, so that the calculated random sequence will not be identical. You can call Srand ((unsigned) time (NULL) before calling the rand () function, so that the time function value (that is, the current times) is the seed number, since the two times when the RAND function is called is usually different, so that the randomness can be guaranteed. You can also use the Srand function to artificially specify the number of seeds. Windows 9x/nt game FreeCell allows the user to specify the number of seeds, so that if a game is not successful, the next time you can play the same licensing results again.

Example:

The following are the referenced contents:

#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
#include <time.h>
void main()
{

srand( (unsigned)time( NULL ) ); //注意不要放在for循环里,否则产生的数是同一个数。
for(int i=0;i<100000;i++)
{

cout<<rand()<<endl;
}
}

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.