Re: [問題] rand()的問題
以下,在 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
討論串 (同標題文章)
完整討論串 (本文為第 3 之 4 篇):
3
4
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章