I'm trying to write a program that generates random numbers from 1-365 and then stores each random number in the array. I've managed to do that thus far but I also need to compare the newly generated random number with the existing array elements and then if they are a match then the loop stops and sets a different Boolean array value to true. I'm unsure of how to complete this.
Please be concise in your answer, I just started learning c++, thanks!
P.S. I'm using windows btw so I'm unable to use the random() function and have to use the rand() instead. Thanks!
#include <iostream>
#include <cstdlib>
usingnamespace std;
int simulate() {
int x, i, a, random;
// boolean bday array and index array
bool bdays[365];
int bdaysIndex[365];
// Setting the Birthday Integer Index to 0
for (a = 0; a <= 365; a++) {
bdaysIndex[a] = 0;
}
// Setting each array value to false
for (x = 0; x <= 365; x++) {
bdays[x] = false;
}
// Try to find a birthday match while birthday match equals false
do {
// while i less than or equal to 365 keep generating random numbers
for (i = 0; i <= 365; i++) {
random = (rand() % 365);
bdaysIndex[i] = random;
cout << "RANDOM Number :" << i << " is: " << random << endl;
cout << bdaysIndex[i] << endl;
}
break;
} while (bdays[i] != true);
return 0;
}
int main() {
cout << "Birthday Match Statistical Analysis" << endl;
simulate();
return 0;
}
Your 3 for loops each make the same small error. They do an out-of-bounds array access on they're last iteration because they refer to element 365 in array of size 365. The highest allowed element in an array of 365 is 364. So all your "x <= 365" should be "x < 365".
Also your first 2 for loops are unnecessary. When initializing all an arrays elements to 0 (or the equivalent of 0 for the elements' data type, with bools 0 is equivalent to false), you can use initializer-list syntax and assign 1 element, the rest are automatically 0-initialized.
1 2
bool bdays[365] = {false};
int bdaysIndex[365] = {0};
First 2 for-loops then not needed.
If you want to generate random numbers between from 1 to X (assuming X is greater than 1) you will need:
1 2
int myInt1 = (rand() % X) + 1; //A number from 1 to X.
int myInt2 = (rand() % X); //A number from 0 to (X-1).
Before anything else happens in main() you should the random-number generator, otherwise all later calls to rand() may or may not work right. The simplest and most common way to seed the rand-number-generator is to #include <ctime> at the top of your file, and at start of main(), call:
srand( time(0) );
I'm a little confused what your trying to do with these arrays tho. You seem to have 1 bool and 1 int for each day of the year (365 days), but then you want each int to be a random number [1..364]. So what are doing with these ints and what are the bools trying to indicate?
Right now, you are generating 365 random numbers and storing them. There is no comparison, and you will always get 365 numbers. From my understanding, you would like to generate numbers until you encounter a duplicate, then break out of the loop. To do this, I suggest getting rid of the Boolean array altogether, and using bdaysIndex as a bucket.
What using bdaysIndex as a bucket means is, after you generate a number, you increment bdaysIndex[thatNumber]. Then, you check to see if bdaysIndex[thatNumber] is >1 (has been incremented multiple times). If it is, you've found your match and can exit.
If that's not what you would like to do, you could use a brute force method, where each time you generate a new number, you loop through your index array, checking whether each element is equal to the new number, and break if one is. This is suboptimal and inelegant, but works nonetheless.