C - srand

C - srand

https://yongqiang.blog.csdn.net/article/details/104429546

Defined in header <stdlib.h> - 定义于头文件 <stdlib.h>

1. srand

void srand (unsigned int seed);

Initialize random number generator - 初始化伪随机数生成器 (函数)

2. Example

2.1 srand

//============================================================================
// Name        : srand
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

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

int main(void)
{
	int random_variable = 0;

	srand(time(0)); // use current time as seed for random generator
	random_variable = rand();
	printf("Random value on [0,%d]: %d\n", RAND_MAX, random_variable);

	return 0;
}

Possible output:

Random value on [0,2147483647]: 1345030193

2.2 srand

//============================================================================
// Name        : srand
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>      /* printf, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

int main()
{
	printf("First number: %d\n", rand() % 100);
	srand(time(NULL));
	printf("Random number: %d\n", rand() % 100);
	srand(1);
	printf("Again the first number: %d\n", rand() % 100);

	return 0;
}

Possible output:

First number: 83
Random number: 66
Again the first number: 83

2.3 srand

//============================================================================
// Name        : srand
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>      /* printf, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

int main()
{
	printf("the first number: %d\n", rand() % 100);
	printf("the second number: %d\n", rand() % 100);
	printf("the third number: %d\n\n", rand() % 100);

	srand(time(NULL));
	printf("random number 1: %d\n", rand() % 100);
	printf("random number 2: %d\n", rand() % 100);
	printf("random number 3: %d\n\n", rand() % 100);

	srand(1);
	printf("again the first number: %d\n", rand() % 100);
	printf("again the second number: %d\n", rand() % 100);
	printf("again the third number: %d\n", rand() % 100);

	return 0;
}

Possible output:

the first number: 83
the second number: 86
the third number: 77

random number 1: 58
random number 2: 10
random number 3: 59

again the first number: 83
again the second number: 86
again the third number: 77

References

https://en.cppreference.com/w/c/numeric/random/srand
http://www.cplusplus.com/reference/cstdlib/srand/

发布了454 篇原创文章 · 获赞 1732 · 访问量 103万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104442131