c++ - How to make Random Numbers unique -


i making random number generator. asks how many digits user wants in number. example enter 2 generate random numbers between 10 , 99. have made generator issue numbers not unique.

here code. not sure why not generating unique number. thought srand(time(null)) it.

void targetgen::randomnumbergen() { srand (time(null)); if (intlength == 1) {      (int = 0; i< intquantity; i++)     {         int min = 1;         int max = 9;         int number1 = rand();          if (intquantity > max)         {             intquantity = max;         }          cout << number1 % max + min << "\t";      }  } else if (intlength == 2) {     (int = 0; i<intquantity; i++)     {         int min = 10;         int max = 90;         int number1 = rand();          if (intquantity > max)         {             intquantity = max;         }          cout << number1 % max + min << "\t";       }  }  if (intlength == 3) {     (int = 0; i<intquantity; i++)     {         int min = 100;         int max = 900;         int number1 = rand();          if (intquantity > max)         {             intquantity = max;         }          cout << number1 % max + min << "\t";     }  } else if (intlength == 4) {     (int = 0; i<intquantity; i++)     {         int min = 1000;         int max = 9000;         int number1 = rand();          if (intquantity > max)         {             intquantity = max;         }          cout << number1 % max + min << "\t";     }  }  if (intlength == 5) {     (int = 0; i<intquantity; i++)     {         int min = 10000;         int max = 90000;         int number1 = rand();          if (intquantity > max)         {             intquantity = max;         }          cout << number1 % max + min << "\t";     }  } else if (intlength == 6) {      (int = 0; i<intquantity; i++)     {         int min = 100000;         int max = 900000;         int number1 = rand();          if (intquantity > max)         {             intquantity = max;         }          cout << number1 % max + min << "\t";       }  }  if (intlength == 7) {     (int = 0; i<intquantity; i++)     {         int min = 1000000;         int max = 9000000;         int number1 = rand();          if (intquantity > max)         {             intquantity = max;         }          cout << number1 % max + min << "\t";     }  } else if (intlength == 8) {     (int = 0; <intquantity; i++)     {         int min = 10000000;         int max = 89999999;         int number1 = rand();          if (intquantity > max)         {             intquantity = max;         }          cout << number1 % max + min << "\t";     }  }  if (intlength == 9) {     (int = 0; < intquantity; i++)     {         int min = 100000000;         int max = 900000000;         int number1 = rand();          if (intquantity > max)         {             intquantity = max;         }          cout << number1 % max + min << "\t";     }  } } 

okay thought figured out way without arrays isn't working before switch fisher yates method. can tell me why isn't working? supposed take random number put variable numgen. in variable b = numgen. hold numgen used when loop goes through , generates random number compare old number , if not equal it, output it. if equal old number rather outputting it, deincrement run through loop without skipping on number entirely. however, when infinitely loops. , not sure why.

if (intlength == 1) {

    (int = 0; i< intquantity; ++i)     {          int min = 1;         int max = 9;         int number1 = rand();         int numgen = number1 % max + min;           if (intquantity > max)         {             intquantity = max;         }          (int k = 0; k < 1; k++)         {             cout << numgen << "\t";             int b = numgen;         }         int b = numgen;         if (b != numgen )         {             cout << numgen << "\t";         }         else         {             i--;         }      }  } 

everyone has interesting expectations random numbers -- apparently, expect random numbers unique! if use good random number generator, random numbers never guaranteed unique.

to make obvious, if wanted generate random numbers in range [1, 2], , generate 2 numbers, (normally expect to) 1 of following 4 possibilities equal probability:

1, 2
2, 1
1, 1
2, 2

it not make sense ask random number generator generate first two, not last two.

now, take second think expect if asked generate three numbers in same range... 1, 2, what??

uniqueness, therefore, not, , not property of random number generator.

your specific problem may require uniqueness, though. in case, need additional work ensure uniqueness.

one way keep tab on numbers picked. can keep them in set, , re-pick if 1 got earlier. however, effective if pick small set of numbers compared range; if pick of range, end of process gets ineffective.

if number count going pick corresponds of range, using array of range, , using shuffling algorithm shuffle numbers around better solution. (the fisher-yates shuffle should trick.)


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -