作者justinC (无)
看板C_and_CPP
标题Re: [问题] 乱数不重复
时间Sat May 23 17:19:05 2009
推 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)
1F:推 chrisdar:我的意思可能没表达清楚 我是指可以参阅该函数的作法 05/23 17:52
2F:→ chrisdar:int count[52]={0}; 通常我写这样 int count[52]={}; 05/23 17:54