I was trying to make a simulation of randomly picking number from 1-36, and to see how many time do I need to pick all the different 36 number. If I picked total of 10 duplicated number I can pick a number that has not been picked, and recount duplication. Anyway, my code works fine if I only create one array contains random numbers. But I add a for loop outside of the whole process and repeat it 100 times in order to find out on average how many time do I need. And the code create 100 of same arrays.
#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;
}
and the result looks like this result all the arrays are the same. I guess it might be the rand() problem, but I can't find any solution that will work.
Aucun commentaire:
Enregistrer un commentaire