c language rand (), srand () function

1. Random number generated by rand():

  • rand(): Generate a random number and return a pseudo-random integer between 0 and RAND_MAX (32767).
  • Although the rand() function can generate a random number, it is not a real random number, but a pseudo-random number.

The so-called "pseudo-random number" refers to not a false random number, but a random number in a hypothetical state obtained through a certain algorithm. The computer can only generate relative random numbers, and these random numbers are both random and regular. , One part obeys certain laws, and the other part does not obey any laws

  • The rand() function needs to call the srand(seed) function when it is used, that is, to provide a seed. If srand() is not used, the system will automatically assign a value to the seed, that is, srand(1).
  • Need to introduce the <stdlib.h> header file when calling rand() and srand()

2. rand() generates a random number in a range

In actual development, what we often need is a random number within a certain range, so our common method is to take the remainder operation and perform an addition operation.

  • To generate a random number between 0 and 99, the usage is as follows:

int num = rand ()% 100;

  • To generate a random number between 1-100, the usage is as follows:

int num = rand ()% 100 +1

  • Think if we like to generate a random number 80-120

int num = rand ()% 41 +80

//用代码演示一下 
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<windows.h>

#include<stdlib.h>
int main(){
    
    
	for (int i = 0; i < 20; i++){
    
    
		int num = rand() % 41 + 80;
		printf("%d\t",num);
	}
	system("pause");
	return 0;
}
//运行结果为
80      97      100     94      102     101     119     82      105     108     86      99      114     97      119
        120     82      91      110     104     请按任意键继续. . .

3 srand() function

  • The header file required by the srand() function is still: <stdlib.h>
  • srand() function prototype: void srand (usigned int seed);
  • The srand() function is to initialize the random number generator. The pseudo-random number generator is initialized with the parameters passed as the seed.

If the same seed is used, the same random number will appear after the rand() function. As mentioned above, the default srand(1) is used to initialize the seed directly, and the random number obtained is still pseudo-random.

  • In actual development, we can use time as a parameter. As long as the time of sowing is different each time, the generated seeds will be different, and the final random number will be different. Usually we use
    the time function in the <time.h> header file. A time accurate to the second can be used as a seed.
//我们在代码中实现随机数结果
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<windows.h>
#include<time.h>
#include<stdlib.h>
int main(){
    
    
	srand(time(0));
	for (int i = 0; i < 20; i++){
    
    
		int num = rand() % 41 + 80;
		printf("%d\t",num);
	}
	system("pause");
	return 0;
}
  • Results of the first run
103     95      113     118     89      91      83      92      99      92      117     97      91      82      97
        91      83      89      91      109     请按任意键继续. . .
  • Results of the second run
81      80      85      84      114     96      110     107     102     83      83      109     113     96      90
        82      105     102     81      114     请按任意键继续. . .


Guess you like

Origin blog.csdn.net/supermanman_/article/details/109201869