jeudi 27 octobre 2016

What is an efficient method to force uniqueness using rand();

If I used (with appropriate #includes)

int main()
 {
   srand(time(0));
   int arr[1000];
   for(int i = 0; i < 1000; i++)
      {
        arr[i] = rand() % 100000;
      }
   return 0;
 }

To generate random 5-digit ID numbers (disregard iomanip stuff here), would those ID numbers be guranteed by rand() to be unique? I've been running another loop to check all the values of the array vs the recently generated ID number but it takes forever to run, considering the nested 1000 iteration loops. By the way is there a simple way to do that check?

Aucun commentaire:

Enregistrer un commentaire