Visual c++ 尝试用随机数生成多个数组,但每次都得到相同的数组
我试着做一个从1-36随机选取数字的模拟,看看我需要多少时间来选取所有不同的36个数字。如果我总共选取了10个重复的数字,我可以选取一个尚未选取的数字,并重新计算重复数。 无论如何,如果我只创建一个包含随机数的数组,我的代码就可以正常工作。但我在整个过程之外添加了一个for循环,并重复了100次,以找出平均需要多少时间。代码创建了100个相同的数组Visual c++ 尝试用随机数生成多个数组,但每次都得到相同的数组,visual-c++,random,Visual C++,Random,我试着做一个从1-36随机选取数字的模拟,看看我需要多少时间来选取所有不同的36个数字。如果我总共选取了10个重复的数字,我可以选取一个尚未选取的数字,并重新计算重复数。 无论如何,如果我只创建一个包含随机数的数组,我的代码就可以正常工作。但我在整个过程之外添加了一个for循环,并重复了100次,以找出平均需要多少时间。代码创建了100个相同的数组 #include "iostream" #include "stdlib.h" #include "time.h"
#include "iostream"
#include "stdlib.h"
#include "time.h"
using namespace std;
class Lottery
{
private:
int PickNonExistingNum()//pick a number that's not in num
{
int pick;
pick = RandomNumber();
for (int m = 0; m < 36; m++)
{
if (pick == num[m])
{
pick = RandomNumber();
m = 0;
continue;
}
}
return pick;
}
public:
int check;
int TimeOfPicking = 0; //how many time we pick a number
int num[36] = { 0 }; //array contains all the number
bool same = false; //flag that indicates consistency
int accum = 0; //how many time we pick a same number, when reach 10 we pick again
bool IsRandomSeeded = false;
int i = 0; //used to change i value in for loop of main loop
int RandomNumber()//generate a number from 1 to 36
{
int number;
number = rand() % 36 + 1;
return number;
}
bool CheckExistingNum()
{
for (int k = 0; k < 36; k++)
{
if (check == num[k])
{
if (accum < 10)//add 1 to accum each time when there's a same number
{
i--;
accum++;
same = true;
break;
}
else if (accum == 10)//pick again when accum reach 10
{
num[i] = DoubleCheckPick(PickNonExistingNum());
accum = 0;
same = true;
break;
}
}
}
return same;
}
void Picking()
{
for (i; i < 36; i++)//finish the loop when player had picked all 36 numbers
{
check = RandomNumber();//give check a random value between 1 and 36
TimeOfPicking++; //add 1 time to the total time of picking
same = false; //set flag to false
if (!CheckExistingNum())//give checked value to num if there's no same number
{
num[i] = check;
}
}
cout << "Pick " << TimeOfPicking << " time" << endl;//show results
cout << "the numbers are:" << endl;
for (int m = 0; m < 36; m++)
{
cout << num[m] << ", ";
}
cout << endl;
}
bool DoubleCheck()
{
for (int m = 0; m < 36; m++)
{
for (int k = m + 1; k < 36; k++)
{
if (num[m] == num[k])
{
cout << "there's a same number: " << num[m] << endl << endl;
return false;
}
}
}
cout << "no same number" << endl << endl;
return true;
}
int DoubleCheckPick(int check)
{
for (int m = 0; m < 36; m++)
{
if (check == num[m])
{
check = PickNonExistingNum();
m = 0;
continue;
}
}
return check;
}
void reset()//reset all the value for multiple process
{
int TimeOfPicking = 0; //how many time we pick a number
int num[36] = { 0 }; //array contains all the number
int same = false; //flag that indicates consistency
int accum = 0; //how many time we pick a same number, when reach 10 we pick again
int check = 0;
int i = 0; //used to change i value in for loop of main loop
}
};
int main()
{
Lottery Num;
//int t = 0;
srand(time(NULL)); //set random generator
int PickingTime[100] = { 0 };//an array contains all 100 times of data
double sum = 0; //the sum of all the data
for (int i = 0; i < 100; i++)//run the program 100 times
{
cout << "the " << i + 1 << " process" << endl;
int wait = clock();
while (clock() <= (wait + 1000));//delay for 3000 unit time
Num.reset();
Num.Picking();
if (!Num.DoubleCheck())//if there's a same number
{
cout << "fail process" << endl;
i--; //redo the process
continue;
}
else
{
PickingTime[i] = Num.TimeOfPicking;
}
}
for (int i = 0; i < 100; i++)
{
sum += PickingTime[i];
}
cout << "the average picking time in 100 test is: " << sum / 100 << endl;
return 0;
}
#包括“iostream”
#包括“stdlib.h”
#包括“time.h”
使用名称空间std;
班级彩票
{
私人:
int picknowenexistingnum()//选择一个不在num中的数字
{
整数选择;
拾取=随机数();
对于(int m=0;m<36;m++)
{
if(pick==num[m])
{
拾取=随机数();
m=0;
继续;
}
}
回采;
}
公众:
整数检查;
int TimeOfPicking=0;//我们选择一个数字的次数
int num[36]={0};//数组包含所有数字
bool same=false;//表示一致性的标志
int accum=0;//我们选择同一个数字的次数,当达到10时,我们再次选择
bool israndomseeed=false;
int i=0;//用于更改主循环for循环中的i值
int RandomNumber()//生成一个从1到36的数字
{
整数;
数字=兰德()%36+1;
返回号码;
}
bool CheckExistingNum()
{
对于(int k=0;k<36;k++)
{
如果(检查==num[k])
{
if(acum<10)//当有相同的数字时,每次向acum加1
{
我--;
accum++;
相同=正确;
打破
}
否则如果(accum==10)//当accum达到10时再次拾取
{
num[i]=DoubleCheckPick(PickNonExistingNum());
累计=0;
相同=正确;
打破
}
}
}
返回相同的值;
}
无效拣选()
{
for(i;i<36;i++)//当玩家选择了所有36个数字时,完成循环
{
check=RandomNumber();//给check一个介于1和36之间的随机值
TimeOfPicking++;//拾取总时间加1次
same=false;//将标志设置为false
if(!CheckExistingNum())//如果没有相同的数字,则为num指定检查值
{
num[i]=检查;
}
}
cout您的reset()
函数实际上没有重置类变量-它定义了新的本地变量。因此,对Picking()的后续调用对i
没有任何作用。将其更改为:
void reset()//reset all the value for multiple process
{
TimeOfPicking = 0; //how many time we pick a number
//int num[36] = { 0 }; //No need to reset the array - it will be overriden in the later invocation of Picking()
same = false; //flag that indicates consistency
accum = 0; //how many time we pick a same number, when reach 10 we pick again
check = 0;
i = 0; //used to change i value in for loop of main loop
}
每次你都会得到不同的数字序列
请注意,您的CheckExistingNum()
也必须被修复才能正常工作-将for(int k=0;k<36;k++)
更改为for(int k=0;k
以仅检查当前循环中生成的数字,而不是旧的数字。这是必需的,因为在reset()
(若要在初始化以外的任何位置将其设置为零,必须使用循环或memset()
)。此外,它还可以加快程序的运行速度
经过两次修复后,我得到了以下输出:
......
the 98 process
Pick 76 time
the numbers are:
26, 9, 4, 36, 32, 12, 16, 7, 17, 33, 1, 10, 18, 2, 23, 21, 28, 29, 30, 25, 8, 34, 27, 31, 14, 24, 11, 20, 6, 22, 3, 15, 19, 5, 13, 26,
there's a same number: 26
fail process
the 98 process
Pick 76 time
the numbers are:
17, 3, 15, 7, 31, 12, 26, 24, 9, 13, 4, 32, 29, 11, 20, 14, 23, 1, 33, 18, 8, 27, 21, 19, 25, 6, 30, 36, 2, 5, 28, 34, 22, 35, 16, 10,
no same number
the 99 process
Pick 106 time
the numbers are:
10, 11, 9, 28, 36, 15, 18, 8, 13, 34, 30, 3, 12, 21, 16, 1, 2, 27, 19, 23, 17, 5, 20, 14, 4, 25, 6, 29, 7, 22, 33, 26, 32, 24, 31, 35,
no same number
the 100 process
Pick 106 time
the numbers are:
15, 23, 22, 11, 5, 35, 2, 26, 21, 4, 3, 33, 28, 19, 1, 14, 7, 29, 24, 32, 8, 12, 13, 27, 10, 17, 34, 18, 16, 6, 25, 20, 31, 36, 30, 9,
no same number
试试看!你可以进一步优化程序,这只是一个练习。:)这很可能是因为周期。你的随机数生成器的周期为36。因此,它倾向于重复自身。你最好尝试更改周期,但每次我调用rand()时,它应该是不同的数字序列,每次我运行程序时,数组与上次不同,但彼此相同。这与随机数生成器无关-您使用的是rand()
正确-看到我下面的答案非常感谢!我试过了,它很有效!但除此之外,我也很好奇为什么数组的开头和结尾总是有两个相同的数字?这与我的代码结构有关吗?在每个函数中添加printf
,或者学习使用断点(我看到了你的visual-c++
标签-visual Studio真的让调试变得很有趣,你应该试一试)。慢慢来,原因就要揭晓了。调试愉快!