Random Number Generation (the random number generated here is a pseudo random number)

Source: Internet
Author: User
Tags random seed

Online, but the source is unknown
Random Number
1. the random () function cannot be used in C ++.
Random functions are not ansi c standards and cannot be compiled by GCC, Vc, or other compilers. But in C Language int random (Num) can be used like this, it returns a random number from 0 to num-1. You can use the rand function in C ++.
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;
# Define max 100 2 W;
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, or 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;
}
Ii. Rand () Usage
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.
/* Maximum value returned by "RAND" Function
*/
# Define rand_max 0x7fffu
This is the definition in bcc55, indicating that the maximum number of this integer is 0x7fffu, and u represents unicode encoding.

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, then it is like this: # K & P * m, y $ S. U "@ 3 K
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.
This type of push.
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 ).
Application of Random Functions in C Language

We may all know the random function random in C language, but the random function is not ansi c standard. Therefore, the random function cannot be compiled by GCC, Vc, or other compilers. How can we implement Random Functions in C language?
In addition to the random function, there is also an rand function, which is also a random function that can generate random numbers from 0 to rand_max.
# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{
Int K;
K = rand ();
Printf ("% d \ n", k );
Return 0;
}
You can compile and run the above Code and find that it does generate a random number, but you will find that the random number is the same every time you run the program. Why? Because the random number is a fixed sequence in the C language, each execution takes the same number.
So how to write a program so that the random numbers generated each time it runs are different? See the following example:
# Include <stdlib. h>
# Include <stdio. h>
# Include <time. h>
Int main (void)
{
Int I;
Time_t T;
Srand (unsigned) Time (& T ));
Printf ("ten random numbers from 0 to 99 \ n ");
For (I = 0; I <10; I ++)
Printf ("% d \ n", Rand () %100 );
Return 0;
}
When you run the program, you will find that the random numbers generated each time are different.
So why is the first program the same and the second program the same?
The second program uses a new function srand, which generates a random seed for a random number. The function prototype is srand (unsigned) Time (null ));
The value of time varies every moment. So the seeds are different, so the random numbers are also different.
Therefore, to generate different random numbers, you must call srand before using Rand.
Since the random number generated by Rand ranges from 0 to rand_max, while rand_max is a large number, how can we generate a random number from X ~ What about the number of Y?
From X to Y, there is a number of Y-X + 1, so to generate the number from X to Y, you only need to write like this:
K = rand () % (Y-X + 1) + X;
In this way, you can generate random numbers in any range you want.

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.