Hello everyone, I'm trying to understand this loop that simulates the number of trials in the birthday paradox and compares the birthdays.
What I don't understand is why the first loop - Number of people in the room - exists. For example,
If I pass
nPpl = 2 //2 people in the room
nTrials = 1 // just 1 trial
this function will perform the second loop - trials loop - nPpl times
which in this case is 2 times. However, I only want it to perform the trails
once with 2 people.
int compare(int bdays[], int nPpl, int &count, int nTrials) {
// Number of People in the room
for (int ppl = 0; people < nPpl; ppl++)
{
count = 0;
// do trials_ number of trials
for (int trial = 0; trial < nTrials; trial++)
{
// function that sets the birthdays
setBdays(bdays, nPp_);
//compare birthdays that are set in i and j
for (int i = 0; i < nPpl; i++) {
for (int j = i + 1; j < nPpl_; j++) {
if (bdays[i] == bdays[j]) {
count++;
}
}
}
}
}
return count;
}
If someone can explain to me if my thought process is correct or incorrect that would be great.
Also, If i switch the trials loop and the number of people loop my output is completely wrong.
for (int trial = 0; trial < nTrials; trial++) //Do this one time
{
..
for (int i = 0; i < nPpl; i++) //do this 2 times...
so these 2 loops cause you to do the deep stuff twice. Is this what you are unhappy about? Does this imply that some of your loops were not supposed to be nested?
// Number of People in the room - if nPpl = 2 this means that the next loop will be executed twice
for (int ppl = 0; people < nPpl; ppl++)
// do trials number of trials - here if nTrials = 1, this loop will still be executed twice since
// the first loop has nPpl = 2
for (int trial = 0; trial < nTrials; trial++)
So, I guess I'm confused because if i switch nPpl and nTrials so that the whole function will loop nTrial times, the result is completely wrong. However, using nPpl, the result is closer to the right result.
// Number of Trials
for (int trials = 0; people < nTrials; trials++)
{
count = 0;
// Number of people
for (int people = 0; people < nPpl; peoplel++)
// function that sets the birthdays
setBdays(bdays, nPp_);
//compare birthdays that are set in i and j
for (int i = 0; i < nPpl; i++) {
for (int j = i + 1; j < nPpl_; j++) {
if (bdays[i] == bdays[j]) {
count++;
}
}
}
}
}
return count;
}
This gives a wrong result but I guess my thinking is.
Should I not loop it nTrialls times and put the number of people loop nested within so that it will execute the amount of trials I want?