One, the random function is not ANSI C standard, cannot compile the pass under the compiler such as GCC,VC. 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.
C + + random function (VC program)
#include <stdlib.h>
#include <iostream.h>
#include <time.h>
#define MAX
void Main ()
{srand (
unsigned) time (NULL)); the//srand () function produces a random seed for
(in T i=0;i<10;i++)
Cout<<rand ()%max<<endl;//max is the maximum, and its random field is 0~max-1
Two, the use of rand ()
rand () does not require a parameter, it returns an arbitrary integer from 0 to the maximum random number, and the maximum random number is usually a fixed large integer. So, if you want to produce a 0~10 10 integers, it can be expressed as:
int N = rand ()% 11;
this way, the value of N is a 0~10 random number, and if you want to produce a 1~10, it is:
int N = 1 + rand ()% 10;
In summary, it can be expressed as:
A + rand ()% n
where a is the starting value and N is the range of integers. To 0~1 a decimal number, you can get 0~10 integers, and then divide by 10 to obtain random to very bit 10 random decimals, to get random decimal digits to percentile, you need to get 0~100 10 integers, then all divided by 100, and other cases according to
this analogy. The
usually the random number generated by rand () is the same as the previous one at each run, intentionally designed to facilitate debugging of the program. To produce a different random number each time, you can use the Srand (seed) function for randomization, which can produce different random numbers, depending on the seed.
As you have said, you can also include the time.h header file, and then use Srand (0) to randomize the random number generator using the current time, so that you can get a different sequence of random numbers every two times (as long as two runs at intervals of more than 1 seconds).
Note: rand () produces a random number on 0 to Rand_max (32767), and 32767 cannot be divisible by 11.
therefore int N = rand ()% 11; The random number that is obtained is not the probability of taking 9, 10 of the probability from 0-10 uniformly distributed, 0-8 is about 1/2978;
so the random number to take 0~m, if M is smaller, this is not a big problem, However, when M is larger, such as m=30000, the probability of taking 0-2767 is twice times of the following number, which is seriously inconsistent with the random distribution.
Three, set the probability on demand
For example, to set a probability problem of 10%, we can take the rand () function to implement, in the if conditional sentence judgement, with the value of rand () a set value, and another value of "= =" operation.
For example:
if (1==rand ()%10)
{//10% probability is reached, here Edit the remaining code}
else
{//90% probability is not reached, edit the remaining code here}