如何生成在某个区间的随机数
2009-03-12 16:05
381 查看
一,需要的库函数和头文件
#include <stdlib.h>
int rand(void);
void srand(unsigned int seed);
二,函数说明
The rand() function returns a pseudo-random integer between 0 and RAND_MAX.
The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value.
If no seed value is provided, the rand() function is automatically seeded with a value of 1.
简单的说rand函数生成一个范围从0到RAND_MAX的随机数,srand是设置随机生成器的种子。
三,使用方法
要求生成一个从区间intBeg到intEnd范围的随机数。
//以当前系统时间作为种子
srand((unsigned int) time(0));
//生成随机数
int randval = intBeg+(int) ((float)(intEnd-intBeg)*rand()/(RAND_MAX+1.0));
#include <stdlib.h>
int rand(void);
void srand(unsigned int seed);
二,函数说明
The rand() function returns a pseudo-random integer between 0 and RAND_MAX.
The srand() function sets its argument as the seed for a new sequence of pseudo-random integers to be returned by rand(). These sequences are repeatable by calling srand() with the same seed value.
If no seed value is provided, the rand() function is automatically seeded with a value of 1.
简单的说rand函数生成一个范围从0到RAND_MAX的随机数,srand是设置随机生成器的种子。
三,使用方法
要求生成一个从区间intBeg到intEnd范围的随机数。
//以当前系统时间作为种子
srand((unsigned int) time(0));
//生成随机数
int randval = intBeg+(int) ((float)(intEnd-intBeg)*rand()/(RAND_MAX+1.0));
相关文章推荐
- rand函数和srand函数的用法和区别+产生指定区间的随机数如何生成
- c++如何生成随机数
- c语言如何生成随机数
- C语言如何生成规定范围内的随机数
- JS如何用Math.random()来生成指定范围内的随机数?
- 如何每5秒生成一个随机数
- C语言区间随机数生成 with srand() & rand() & time() (转)
- 生成某一区间的随机数
- JS如何用Math.random()来生成指定范围内(如:1-100)的随机数?
- c语言中如何生成1个0到1的随机数
- 生成区间内互不重复的随机数序列
- C语言随机数,如何生成既有正数又有负数的随机数呢
- 如何生成随机数
- 如何用C#生成不重复的随机数
- java中的随机数(Math.random()),以及如何产生一个类似于[21,77]区间的随机数。
- 如何生成一个32位的随机数
- 生成区间[a,b)中的随机数
- 在指定区间内,生成K个随机数
- C语言区间随机数生成 with srand() & rand() & time() (转)
- c++如何生成随机数