Re: [問題] rand()的問題
以下的程式,可以解決 一謝問題
一 一定要 執行srand()
二 srand() 是用來設定亂數的 種子數,
為了確保使用與上次 不同的種子數,
犧牲一下 程式執行的速度
三 為了避免時間很接近的問題,讓前面
的 三百個亂數 虛耗掉
// by Dev-C++ 4.9.9.2
#include <cstdlib>
#include <iostream>
using namespace std;
// ----------------------------------------------
int main(int argc, char *argv[])
{
int a, b, i, t1, t2;
t1= t2= time(NULL);
while (t1 == t2) {
t1= time(NULL);// get new time
}
srand((unsigned) t1);
for (i=0;i<300;i++) {
a= rand();
}
// ------------------------------------------
for (i=0;i<10;i++) {
a= rand();
b= rand();
cout << a << ", " << b << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.231.121.250
→
05/14 22:29, , 1F
05/14 22:29, 1F
→
05/14 23:45, , 2F
05/14 23:45, 2F
→
05/14 23:57, , 3F
05/14 23:57, 3F
→
05/15 00:02, , 4F
05/15 00:02, 4F
討論串 (同標題文章)
完整討論串 (本文為第 4 之 4 篇):
3
4
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章