Hello! I programmed a little game, which runs a while loop for a couple of seconds, that is constrantly generating random numbers and at the end, the program outputs the most commonly generated number. My problem is that everytime I run the program, the most common number is always 90.
Code:
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main() {
time_t end = time(NULL) + 5; // time
int retVal = 0;
int mcn = 0;
int arr[90];
for(int x = 0; x < 90; x++) {
arr[x] = 0;
}
srand(time(NULL));
while (time(NULL) < end) {
int n = rand() % 90;
cout << n << endl;
for(int x = 0; x < 90; x++) {
if(arr[x] == n)
arr[x]++;
}
}
for(retVal = 0; retVal < 90; retVal++) {
if(arr[retVal] > mcn)
mcn = arr[retVal];
}
cout << "The most common number is: " << mcn<< endl;
return 0;
}