[C Language] Detailed explanation of random number rand()

1.The nature of random numbers

In fact, the random numbers generated by the rand() function are pseudo-random numbers, which are calculated according to a certain formula based on a numerical value. This numerical value is called a "seed". The relationship between seeds and random numbers is a normal distribution, as shown in the figure below:

There is a normal distribution between random numbers and seeds in C language

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

2. Examples of generating random numbers

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);

There is also a random() function in the C language that can obtain random numbers, but random() is not a standard function and cannot be passed by compilers such as VC/VS, so it is rarely used.

rand() will randomly generate an integer between 0 ~ RAND_MAX.

The following is an example of random number generation:

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

3. Generate random numbers using timestamps

We can re-seed through the srand() function, so that the seed will change. 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 sowing time is different each time, the generated seeds will be different, and the final random numbers will also be different.

Use the time() function in the <time.h> header file to get the current time (accurate to seconds), as follows:

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;
}

4. Generate random numbers within a certain range

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

int a = rand() % 10;    //产生0~9的随机数,注意10会被整除

If you want to specify upper and lower limits:

int a = rand() % 51 + 13;    //产生13~63的随机数

Analysis: Modulo is the remainder. rand()%51+13We can think of it as two parts: rand()%51it generates random numbers from 0 to 50, and then +13it is guaranteed that the minimum a can only be 13, and the maximum is 50+13=63.

example:

The complete code to generate random numbers in the range of 13~63:

#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;
}

Guess you like

Origin blog.csdn.net/Daears/article/details/127424054