[Random number] rand() function and srand() function in C language

Table of contents

One: rand() function

Two: srand() function 

Among them: the difference between srand((unsigned)time(NULL)) and srand((unsigned)time (0)?

Three: time() function

Four: After understanding the above three functions, how to use them?

rand() function random number generation range:


One: rand() function

rand() function: 1. Used to generate random numbers in C language

                    2. The required header file: stdlib.h

                    3. There is a macro #define RAND_MAX 0x7fff in the stdlib.h header file, which will return a random number in the range 0-0x7fff, that is, a number up to 32767

                     4. The prototype of the rand() function

#include <stdlib.h>

int rand(void);

                      5. The call of the rand function 

The rand() function will check whether srand(seed) has been called before each call, and whether a value has been set for the seed. If so, it will automatically call srand(seed) once to initialize its initial value. If srand(seed) has not been called before, the system will automatically assign an initial value to seed, that is, srand(1) will automatically call him once. (Before calling the rand() function, you can use the srand() function to set the random number seed. If the random number seed is not set, the rand() function will automatically design the random number seed to be 1 when the rand() function is called. The random seed is the same, and each generation will be the same random number) 

Two: srand() function 

srand() function: 1. Required header file: <stdlib.h>

                       2. srand() function prototype:

#include <stdlib.h>

void srand (usigned int seed);

                       3. This function needs to provide a seed

If the seeds are the same, the generated random numbers are also the same. In order to randomize the random numbers, set a re-changed seed, such as:

use time as seed

srand(time(NULL))

When we generate random numbers, when using rand and srand functions, the main initialization method

srand((unsigned int)time(NULL))

We often use the system time to initialize, use the time function to obtain the system time, the obtained value is a timestamp, that is, the number of seconds from 0:00 on January 1, 1970 to the current time, and then convert the obtained time_t type data into (unsigned int) type, and then passed to the srand function.

If you still feel that the time interval is too small, you can multiply an appropriate integer after (unsigned)time(0) or (unsigned)time(NULL). For example, srand((unsigned)time(NULL)*10).

There is another way to initialize the seed:

Use the pid of the process as the seed value seed. In the same program, the value of such a seed is the same.

srand((unsigned int)getpid())

(PID (Process Identification) refers to the process identification number in the operating system, that is, the process identifier. Every time a program is opened in the operating system, a process ID, namely PID, will be created.

PID is the code name of each process, and each process has a unique PID number. It is allocated by the system when the process is running and does not represent a specific process. The PID will not change the identifier during runtime, but the PID identifier will be reclaimed by the system after the process terminates, and may continue to be assigned to the newly running program. ) Inner os: It should be like this, my Baidu pid.

Among them: the difference between srand((unsigned)time(NULL)) and srand((unsigned)time (0)?

One is srand (unsigned int);
the other is time (long *t);
here is to write them together, the return value of the time function is used as the parameter of the srand function, and NULL is the input parameter of the time function, the time function The parameter is required to be a pointer, so NULL must be used. NULL means that the pointer does not point to any variable, and 0 is of int type. If the type does not match, if you have to
use 0, you can also perform forced type conversion. srand((unsigned)time ( ( long *) 0)) ;

Three: time() function

time() function: 1. The required header file <time.h>

                    2.time() function prototype:

time_t time(time_t *tloc);//time_t type is defined as a long integer

Four: After understanding the above three functions, how to use them?

rand() function random number generation range:

[0-99]

int num = rand() % 100;

[1,100]

int num = rand() % 100 + 1;

Finally, the difference between +1 and not +1, the minimum value of +1 is 1, and the minimum value of not +1 is 0

[3,9]

int 3+rand () % 7;

Summarize:

[m,n]

int m+rand()%(n-m+1);

To make the random number random, remember to quote the srand function~ 

Please correct me if I am wrong~

Guess you like

Origin blog.csdn.net/m0_57549888/article/details/124095587