![]() |
non-repetitive random number
hi guys,
when i generate k random numbers in range from 1 to 100, usually i got a few numbers that repeat themselves. how can i generate the k non-repetitive random number? i tried to do error checking, whether the new generated random number is the same with the old one, but it's not working thanks |
Aside from using a method which will incur some overhead, you can't. And if it's random, you should allow for the possibility of having a duplicate. You could keep a list of previous numbers and check for a duplicate if you wanted to. Or, you could keep a pool of valid numbers, shuffle it up, and remove randomly indexed values from the shuffled list.
|
or you can do something like...
:
for(i=0;i<100;++i) |
Quote:
|
If you are dealing a deck of cards, infogeek's method is what you want. You can deal a card only once. If you are doing something with "random" selections, then repeats have to be allowed, because that is what random means.
|
| All times are GMT -5. The time now is 1:56 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC