Know the random function rand () and srand (unsigned int)

The rand function

 

int rand( void );

Function name: rand  

Function: random number generator

Usage: int rand (void); 

Where the header file: stdlib.h

Function Description:

Internal rand () implementation is done using a linear congruential method, it is not really random, because of their particularly long period, so that a certain

In the range it can be regarded as random.

rand () returns a random number value in a range between 0 to RAND_MAX. RAND_MAX range is between a minimum of 32767 (int). use

unsigned int double-byte is 65535, is an four-byte integer ranging 4294967295. 0 ~ RAND_MAX each number is selected

The probability is the same.     

When the user does not set the random number seed, the default is a random number seed.

rand () is a pseudo-random number generated, each execution is the same; to different () function initializes it with the srand.

 

This function is a non-parametric function, the return value is a random number of type int, a value in the range [0, 0x7FFF] ie [0, 32767]. Although this function takes no arguments, but the rand function is required to run a seed (seed), seeds () function is provided by srand. The former we srand function will be described .rand function generates a random number, it is necessary to generate a pseudo random number sequence provided by the seed system, according to the random number RAND to generate a series of values ​​of the seed. If the seed is not provided by the system change, each pseudo random number sequence generated by the rand function calls are the same. srand (unsigned seed) Seed seed value provided by changing the parameters of the system, so that the pseudo-random number sequences may each call rand function generating different so that "random" in the true sense. Typically the system can be used to change the system time of the seed value, i.e. srand (time (NULL)), may provide a different seed value rand function, thereby generating a different sequence of random numbers.

srand function

 

void srand(unsigned seed);

Function name: srand  

Function: initialize the random number generator  

用   法:   void srand(unsigned  int  seed); 

Where the header file: stdlib.h 

Function Description:

srand () to set the rand () generates a random number at the random number seed.

Seed parameter must be an integer, typically can be utilized time (0) to return NULL value or as a seed.

If each seed are set the same value, rand () random number value generated will be the same each time.

 

srand and rand () with the use of pseudo-random number sequence. As long as the parameters of this function is changed, the rand function can generate random values, if the parameter of the function is a fixed value, so every time a random number function rand function on the same value and the last produced. If we need multiple calls rand function, resulting in different values ​​with, we need to give him a different set of seed.

 

Third, the relationship between the rand () and srand () in

rand () and srand () to be used together, wherein srand () is used to initialize the random number seed, rand () is used to generate a random number.

Because the random number seed as the default 1, and the same random number generated by the random number seed is the same, lost the meaning of randomness, so to get the random number is not the same every time, with the function srand () to initialize the random the number of seeds. srand () parameter value with a time function (i.e., the current time), because the two calls rand () function of time, usually different, so as to ensure the randomness.

 

Fourth, generates the general formula represents a range of random numbers

To obtain [a, B) of the random integer, using (rand ()% (ba)) + a (containing a free result value b).

To obtain [a, B] of the random integer, using (rand ()% (b-a + 1)) + a (result value comprising a and b).

To obtain (a, B] of the random integer, using (rand ()% (ba)) + a + 1 (excluding a result value containing b).

(In general, the general formula: a + rand ()% n; wherein A is a starting value, n being an integer in the range)

To obtain a random integer between the b, another represents: a + (int) b * rand () / (RAND_MAX + 1).

To obtain the floating point number between 0 and 1, may be used rand () / double (RAND_MAX).

 

Five, causes the same random number

The computer is a random number by a pseudo-random number that is generated by a small sequence of polynomial M, wherein each generated sequence has a small initial value, i.e. the random seed. (Note: Small M period sequence polynomial is 65535, that is, each using a random seed generated random number is 65535 cycles, when you get 65,535 random numbers are repeated there.) 

We know that rand () function can be used to generate random numbers, but this is not a random number on Barbara sense, is a pseudo-random number is based on a number (we can call it the seeds) as a reference to a recurrence formula extrapolated series number, when a large number of series this time, it is in line with normal announcement, which is equivalent to generate a random number, but this is not true random numbers, when the computer boots up, the value of this seed is given up, unless you destroy the system.

 

Example one:

#include<stdio.h>

include<stdlib.h>

int main ()

{

int i;

unsigned int seed;

int input;

for(i=0;i<10;i++)

{

scanf("%d",&input);

seed = (unsigned)input;

srand(seed);

printf("Random value is %d\n",rand());

}

return 0;

}

After running the above program, we also found that we can not guarantee the value of each input seed and absolutely not the same as last time. So we use time as a seed, because the current time is always changing, the passage of time or distance is always changing. Before we can use the current time as the seed.

Example 2:

#include<stdio.h>
#include
<stdlib.h> #include<time.h> int main() { int i; time_t t; srand ((unsigned) Time ( & T));

// or
srand ((unsigned) time (NULL )); // initialize the random number seed 

for (I = 0 ; I < 10 ; I ++ ) { printf("Random value is %d\n",rand()); } return 0; }

 

 

 

Generating a random number within the specified interval

In actual programming, we do not always need [0, 0x7fff] all numbers in the interval, we may only need a portion in which a random number [0, 15] within.

 

To obtain [a, B) of the random integer, using (rand ()% (ba)) + a (containing a free result value b).

To obtain [a, B] of the random integer, using (rand ()% (b-a + 1)) + a (result value comprising a and b).

To obtain (a, B] of the random integer, using (rand ()% (ba)) + a + 1 (excluding a result value containing b).

(In general, the general formula: a + rand ()% n; wherein A is a starting value, n being an integer in the range)

To obtain a random integer between the b, another represents: a + (int) b * rand () / (RAND_MAX + 1).

To obtain the floating point number between 0 and 1, may be used rand () / double (RAND_MAX).

/ * Get the interval [2,99] random number * / 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h>
 #define A 2
 #define B 99
 int main ()
{
    int i;
    time_t t;
    srand((unsigned)time(&t));
    for(i=0;i<10;i++)
    {
        printf("%d\t",(rand() % (b-a+1))+ a);
    }
    return 0;
}

 

 

 

Generates a random float

RAND / (Double) the RAND MAX; may be randomly generated [0.0, 1.0] in the decimal range, RAND / (Double) (the RAND MAX +. 1) may generate a decimal [0.0, 1.0) section. Above in an arbitrary interval number generating method, we can randomly generated within an arbitrary section of the float (Note: The first rand () to generate [M, N). X1 integer in the interval, the second rand () to generate [0.0, 1.0] of the float, resulting in the random [M, N] in the float section FloatValue = x1 + x2).

Four examples:

#include <stdio.h> 
#include <stdli.h> 
#include <time.h>
 int the Sentinel = 0 ; // set sentinel, provided to ensure that only one seed 
Double RandomFloate ( int M, int N)
{
int RandomValue1;
double RandomValue2;
if(Sentinel == 0)
{
time_t t;
srand((unsigned)time(&t));
}
RandomValue1 = M + rand() % (M - N + 1);
RandomValue2 = rand() / (double)(RAND_MAX + 1);
return RandValue + RandomValue2;
}
int main ()
{
    int i;
    for(i= 0;i<100; i++)
        {
            printf("%f\t",RandomFloate(2,5));
        }
}

The rand function Mistakes

rand () function sets a seed can be used multiple times. Do not be such a combination of srand ((unsigned) time (& t)) and rand () in a loop, because the time () function is the second unit, may your program in one second on the run n times, then this n times the seed set on the same random number generator is the same. Also note that you do not call srand () function, the system default you call srand (1).

 

 

 

 

Guess you like

Origin www.cnblogs.com/mhq-martin/p/11913515.html