随机数小问题

时间:2008-08-27 23:01:33   来源:论坛整理  作者:  编辑:chinaitzhe
兄弟俺写了如下代码,没用srand(seed) ,生成的数也是随机的啊,那传说中的种子是干什么的?
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
for(int j = 0 ; j < 100 ; j ){
int i = rand(); //随机数
cout < < i < < " ";
if(j % 10 == 0)
cout < < "\n";
}
}
18467 6334 26500 19169 15724 11478 29358 26962 24464 5705
28145 23281 16827 9961 491 2995 11942 4827 5436 32391
14604 3902 153 292 12382 17421 18716 19718 19895 5447
21726 14771 11538 1869 19912 25667 26299 17035 9894 28703
23811 31322 30333 17673 4664 15141 7711 28253 6868 25547
27644 32662 32757 20037 12859 8723 9741 27529 778 12316
3035 22190 1842 288 30106 9040 8942 19264 22648 27446
23805 15890 6729 24370 15350 15006 31101 24393 3548 19629
12623 24084 19954 18756 11840 4966 7376 13931 26308 16944
32439 24626 11323 5537 21538 16118 2082 22929 16541

网友回复:你把这组数字记下来,然后把程序关闭,再运行一次看看生成的结果和这组数字是否相同

随机数种子是指,设定了一个种子(比如当前系统时间)以后,每次运行的时候会根据这个种子来产生随机数,这样就能使每次运行的结果都不同了
网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





//比如:

srand(time(0));//根据系统时间设置随机数种子




网友回复:你的程序下次再运行还是你列出来的结果!!

这样做:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





#include <iostream>

#include <cstdlib>

using namespace std;

int main() {

    srand(time(NULL));

        for(int j = 0 ; j < 100 ; j  ){

int i = rand();    //随机数

cout << i << " ";

if(j % 10 == 0)

cout << "\n";

}

}






网友回复:好,谢谢 ,,我知道了,刚学,多谢支持。
关键字:随机数,问题,

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面