Re: [問題] 亂數不重複
推 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
05/23 17:54, 2F
討論串 (同標題文章)