![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2007
Posts: 1
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
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.
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
or you can do something like...
for(i=0;i<100;++i)
a[i] = i+1;
for(i=0;i<100;++i)
swap(a[i], a[rand()%100]);
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#4 | ||
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 333
Rep Power: 3
![]() |
Quote:
__________________
Quote:
|
||
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
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.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Median/Mode in arrays? {Need help} | Java|Tera | Java | 27 | Nov 29th, 2005 11:50 AM |
| FiveDigit + RandomeNumber Game. | TecBrain | Java | 0 | Nov 18th, 2005 3:53 PM |
| Random Number | coldDeath | Python | 1 | Sep 11th, 2005 9:17 AM |
| Random Number & Average Problem | Hadrurus | Java | 6 | Aug 15th, 2005 2:08 PM |
| non repeating random number generation | gencor45 | C# | 2 | Feb 9th, 2005 1:11 AM |