Re: [問題] rand()的問題

看板C_and_CPP (C/C++)作者 (sjgau)時間16年前 (2009/05/14 17:34), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/4 (看更多)
以下,在 VC++ 6.0 測試過,OK! #include "stdafx.h" // for VC++ 6.0 only #include <stdlib.h> #include <time.h> #include <process.h> #include "iostream.h" // ---------------------------------------------- int main(int argc, char* argv[]) { srand((unsigned) time(NULL)); // 這行是一定要的 for (int i=0;i<10;i++) { int a= rand(); int b= rand(); cout << a << ", " << b << endl; } return 0; } ※ 引述《QQ29 (我愛阿蓉)》之銘言: : 請教各位! : 使用rand()如果我沒有用先呼叫srand()的話 : for(int i=0;i<10;i++) : { : int a=rand(); : int b=rand(); : cout<<a<<","<<b<<endl; : } : 為什麼他這樣我執行好幾次程式他印出來的 sequence 都會一樣 : 是為什麼呢? : 是因為都同一張亂數表 然後他依照順序回傳數值回來嘛? : 另外想問一下 -1.#IND 是什麼意思呢? : -1.#INF 是除以零 可是不知道IND是什麼耶 而且為什麼是 -的呢? : 因為找不到bug= = : 我有一行程式寫 : int B=4; : G2[i] = (float)((rand() % (B + B)) - B) / B; : 有時候G的值會出現-1.#IND 但是我就是不懂 怎麼會發生這種事情... : 而且不是每次都出現@@ : 謝謝各位 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.231.83.251
文章代碼(AID): #1A2-MbYA (C_and_CPP)
文章代碼(AID): #1A2-MbYA (C_and_CPP)