srand
来源:互联网 发布:程序员笔试题 编辑:程序博客网 时间:2024/06/05 00:18
srand
functions
<cstdlib>
void srand ( unsigned int seed );
Initialize random number generator
The pseudo-random number generator is initialized using the argument passed as seed.For every different seed value used in a call to srand, the pseudo-random number generator can be expected to generate a different succession of results in the subsequent calls torand.
Two different initializations with the same seed, instructs the pseudo-random generator to generate the same succession of results for the subsequent calls torand in both cases.
If seed is set to 1, the generator is reinitialized to its initial value and produces the same values as before any call torand or srand.
In order to generate random-like numbers, srand is usually initialized to some distinctive value, like those related with the execution time. For example, the value returned by the functiontime (declared in header <ctime>) is different each second, which is distinctive enough for most randoming needs.
Parameters
- seed
- An integer value to be used as seed by the pseudo-random number generator algorithm.
Return Value
(none)Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* srand example */#include <stdio.h>#include <stdlib.h>#include <time.h>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;}
Output:
First number: 41Random number: 13Again the first number: 41
See also
randGenerate random number (function)- srand()
- srand
- srand ()函数
- rand & srand
- srand() 函数
- srand无效?
- Srand使用
- srand 函数
- Srand函数
- srand函数
- rand()&&srand()
- srand函数
- srand rand
- srand()函数
- rand() && srand()
- srand函数
- 随机函数srand()
- srand() rand()函数
- C# 注释及说明
- 【linux】bash重定向操作符
- poj1191(记忆化搜索)
- Hibernate过滤器使用窍门
- 生命需要思考
- srand
- PhoneGap,sencha touch
- 安全性测试修复意见总结
- oracle 视图、同义词、序列
- Ubuntu 下code::blocks更改程序运行终端
- C预处理器和C库-学习笔记
- MySQL使用LAST_INSERT_ID()获取新插入记录的ID
- C#编写高质量,规范,高效代码资料
- 再谈蚂蚁的问题