[Programming] Introduction to C language: Generate random numbers (not fixed random numbers)

Introduction to C language: generating random numbers


In actual programming, we often need to generate random numbers.

1.rand() function

In C language, we generally use the rand() function in the <stdlib.h> header file to generate random numbers. Its usage is:

int rand (void);

rand() will randomly generate an integer between 0 and RAND_MAX.
RAND_MAX is a macro in the <stdlib.h> header file, which is used to specify the maximum value of the random number that rand() can return. The C language standard does not specify the specific value of RAND_MAX, only that its value is at least 32767. In actual programming, we don't need to know the specific value of RAND_MAX, just treat it as a large number.
Insert picture description here
Run a few more times, you will find that the random number generated each time is the same.
Insert picture description here
In fact, the random number generated by the rand() function is a pseudo-random number, which is calculated according to a certain value according to a certain formula. This value is called " seed". The relationship between seed and random number is a normal distribution.

The seed is random every time the computer is started, but once the computer is started, it does not change; that is, every time the computer is started, the seed is a fixed value, so the result calculated according to the formula (that is, the generated Random number) is fixed.

2.srand() function

The srand() function is used to set the seed for the rand() function. The usage of srand() is:

void srand (unsigned int seed);

It requires a parameter of type unsigned int. In actual development, we can use time as a parameter. As long as the time of each sowing is different, the generated seeds will be different, and the final random number will also be different.
Use the time() function in the <time.h> header file to get the current time (accurate to the second), as shown below:

srand((unsigned)time(NULL));

Modify the above code to seed before generating random numbers:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
    
    
    int a;
    srand((unsigned)time(NULL));
    a = rand();
    printf("%d\n", a);
    return 0;
}

Insert picture description here

Run the program several times and you will find that the random number generated is different each time. However, these random numbers will gradually increase or gradually decrease. This is because we use time as the seed, and time is gradually increasing. Combined with the normal distribution diagram above, it is easy to infer that random numbers will gradually increase. Increase or decrease.

3. Generate random numbers within a certain range

In actual development, we often need random numbers within a certain range, too large or too small does not meet the requirements, then how to generate a certain range of random numbers? We can use the modulo method:

int a = rand()% 10; //Generate a random number from 0 to 9, note that 10 will be evenly divisible

If you want to specify the upper and lower limits:

int a = rand()% 51 + 13; //Generate a random number from 13 to 63

Analysis: taking the modulus means taking the remainder, and rand()%51+13 can be regarded as two parts: rand()%51 is a random number generated from 0 to 50, and the following +13 guarantees that a can only be 13 at minimum and 50 at maximum +13=63.
Finally, the complete code for generating random numbers in the range of 13~63 is given:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
    
    
    int a;
    srand((unsigned)time(NULL));
    a = rand() % 51 + 13;
    printf("%d\n",a);
    return 0;
}

For more explanation, see http://c.biancheng.net/view/2043.html

Guess you like

Origin blog.csdn.net/m0_46613023/article/details/114778131