Chinaunix

标题: 请教:产生随机号码函数rand()? [打印本页]

作者: herocsz    时间: 2009-05-08 16:39
标题: 请教:产生随机号码函数rand()?
我定义了随机号码变量是:randomnum

我是这么做的,可是randomnum 总是为0,为什么?
srand((unsigned int)(time(NULL)));
randomnum = rand();

请问还有随机号码实现的函数吗? 谢谢!
作者: dreamice    时间: 2009-05-08 16:51
man rand
作者: herocsz    时间: 2009-05-08 17:13
哦......
作者: herocsz    时间: 2009-05-08 17:35
我后来单独编了一个测试程序,出来是随机的,不一样的值,可能我在转换的时候弄错了
作者: herocsz    时间: 2009-05-08 17:48
这个是对的,是我自己看错了  ,呵呵  不好意思!
作者: 紫箩    时间: 2009-05-08 20:34
#inlude<stdilib.h>
#include<time.h>  //头文件

srand((unsigned) time(0));
t=rand()%100;//t为产生的随机数,该数的范围为0~100之间.

不知道对不对,可以试一下.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2