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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章