[C language] Random number functions rand and srand

Article Directory

1. Random number function

1.rand()、srand()

2.time()

2. Case realization

1. Case description

2. Code implementation


1. Random number function

1.rand()、srand()

The functions used in C language to generate random numbers are rand() and srand() . The random() function does not follow the ANSIC standard and cannot be compiled under compilers such as gcc and vs.

(1) If it is to generate a random number with no range limit, just use rand().

rand() returns a random number in the range of 0~RAND_MAX. RAND_MAX is defined in stdlib.h and its value is 2147483647.

(2) If you want to generate a random number within a certain range, there are two situations:

① Random numbers starting from 0, for example: generate a random number from 0 to 10, use the rand() function to calculate the remainder of 10, that is, rand()%10;

② Random numbers that do not start from 0, for example: generate a random number from 5 to 25, use the rand() function to calculate the remainder of 25-5=20 and add 5, that is, rand()%20+5.

(3) The random numbers generated in the above two cases are all one-off, no matter how many times after running, the output result will be the same as the first time. In order for the program to generate a new sequence of random values ​​each time it is executed, a new random seed needs to be provided to the random number generator. At this time, you need to use the srand() function, which sows seeds for the random number generator. As long as the seeds are different, the rand() function will generate different random number sequences. srand() is called the initializer of the random number generator.

Function prototype: void srand(unsigned int seed);

The parameter seed of this function is the seed, which is used to initialize the starting value of rand().

The function is: starting from the seed specified in srand (seed), return a random integer between [0,RAND_MAX]. The rand() function is a true random number generator, and srand() provides a random number seed for rand(). srand((unsigned int)time(NULL)) means to use the value of the system timer as the random number seed.

The system will automatically call the srand() function before calling the rand() function. If the user does not call the srand() function before calling the rand() function, the system will default to 1 as the initial value of the pseudo-random number. If the user has called srand() and assigned a value to the parameter seed, the rand() function will use this value as the initial value for generating random numbers. If the seed is given a fixed value, the random numbers generated by the rand() function will be the same.

2.time()

Usually use the system time for initialization, that is, use the time() function to get the system time, and its return value is of type time_t, which must be converted to an unsigned int type and then passed to the srand() function. To use the time() function, you also need to call the "time.h" header file.

When using the time() function, the parameter is generally NULL, that is, a null pointer can be directly passed in. If you think the time interval is too small, you can multiply it by an appropriate integer, for example: srand((unsigned int)time(NULL)*5).


2. Case realization

1. Case description

In a game, two people take turns to roll the dice 5 times, and the points of each dice roll are accumulated. After 5 rounds, the party with the larger accumulated points wins, otherwise it is a tie.

2. Code implementation

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    srand((unsigned int)time(NULL));//使用系统计时器的值作为随机种子
    int res1 = 0, res2 = 0;

    for (int i = 1; i <= 5; i++) {
        res1 += rand() % 6 + 1;//生成1~6的随机数,并进行累加  
        res2 += rand() % 6 + 1;//生成1~6的随机数,并进行累加       
    }
    printf("甲方点数:%-5d 乙方点数:%5d\n", res1, res2);
    if (res1 > res2) {
        printf("甲方获胜\n");
    }
    else if (res1 < res2) {
        printf("乙方获胜\n");
    }
    else {
        printf("平局");
    }
}

 

Guess you like

Origin blog.csdn.net/Jacky_Feng/article/details/108920377