What we know:
-
I have to generate M sets of N random numbers, each number being unique with respect to the other N-1 numbers in the same set. With respect to the other M-1 sets, the numbers can repeat sometimes.
-
Every number must take a value between 1 and 90.
-
I am using the
rand()function ofstdlib.hand seeding it withsrand(time(NULL)).
Problem:
M usually takes a value between 100 and 1000 and with the current implementation, the algorithm only generates 2 (sometimes even 1) sets of random numbers, each one repeating 50% of times. So if M = 100 we will have SET1 SET2 SET1 SET2 ... SET1 SET2, with SET1 50 times and SET2 50 times, instead having the desired output: SET1 SET2 SET3 ... SET99 SET100.
What I've tried:
So far, I've used a frequency array (array that retains the number of times a number appears in a data structure) that holds how many times any number from 1 to 90 have been generated in 1 set. If it has already been generated, it tries to generate another one, and so on. This is the function:
int randomNumber(int frequency[]) {
int x = rand()%90 + 1;
if(frecquency[x] != 0)
while(frequency[x] != 0)
x = rand()%90 + 1;
return x;
}
This function works as intended, it is able to generate N unique random numbers between 1 and 90. It just generates the same numbers always!
Aucun commentaire:
Enregistrer un commentaire