Re: [問題] 亂數不重複

看板C_and_CPP作者 (無)時間15年前 (2009/05/23 17:19), 編輯推噓1(101)
留言2則, 1人參與, 最新討論串2/3 (看更多)
推 LPH66:我覺得問題出在你每次洗牌都srand一次... 05/23 17:04 → LPH66:srand在整份程式裡只要出現一次在main的開頭即可... 05/23 17:05 感謝幫忙!我當時有想過這個問題,可能忘了拿掉 於是拿掉後測試,則不會出現太過集中的問題 但還不太敢肯定是否沒問題 推文中有人提到random_shuffle函式 我拿來測試之後,我的方法與系統提供的方法數值很接近 大致上已經夠亂,我的問題應該告一段落 random_shuffle程式碼如下: #include <string> #include <vector> #include <algorithm> #include <iostream> using std::random_shuffle; using namespace std; int main() { vector<int> vi; int count[52]={0}; for (int i = 0; i < 52; i++) vi.push_back(i+1); for(int i =0 ; i < 52 ; i ++){ random_shuffle(vi.begin(), vi.end()); //打亂 for (int i = 0; i < 52; i++) //統計 if(vi[i] == 1 ) count[i]++; } for (int i = 0; i < 52; i++) cout<<"count["<<i<<"]="<<count[i]<<endl; system("PAUSE"); } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 125.229.167.196 ※ 編輯: justinC 來自: 125.229.167.196 (05/23 17:22)

05/23 17:52, , 1F
我的意思可能沒表達清楚 我是指可以參閱該函數的作法
05/23 17:52, 1F

05/23 17:54, , 2F
int count[52]={0}; 通常我寫這樣 int count[52]={};
05/23 17:54, 2F
文章代碼(AID): #1A5x-AG0 (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1A5x-AG0 (C_and_CPP)