In C language, the random function random and the random function rand in C ++.
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 ~ Random Number of 10,
If you want to generate 1 ~ 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.
0 ~ 1 decimal point, you can first get 0 ~ And divide all the values by 10 to obtain the 10 random decimal places ranging from random to very digits,
To obtain a random decimal number from random to percentile, you must first obtain 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, you can also include the 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 ).
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.
// C ++ random function (VC Program)
# Include <stdio. h>
# Include <iostream> // The standard library <cstdlib> (included in <iostream>) provides two functions to help generate pseudo-random numbers:
# 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
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;
}
Source: http://zhidao.baidu.com/question/23111502.html? Fr = ala0
See http://www.cnblogs.com/chenglei/archive/2009/07/01/1514689.html
(Double) rand ()/rand_max generates a 0 ~ The double number between 1.