Is this impossible? I want the program to pick a random number from 1-20 and then add that to a list. Then I want it to do it again, this time checking to see if the number chosen is already added to the list, and if so, choosing another one. This way the list has no repeats.
I've been trying to make this work for awhile now, but I can't figure it out. Any ideas, or should I use a "real" programming language?
Offline
You might be doing this backwards.
Download my blackjack game to see how the deck of cards is managed.
It needs to be able to draw every card in the deck without drawing the same card twice.
Hope this helps.
Offline
Faldwin wrote:
Is this impossible? I want the program to pick a random number from 1-20 and then add that to a list. Then I want it to do it again, this time checking to see if the number chosen is already added to the list, and if so, choosing another one. This way the list has no repeats.
I've been trying to make this work for awhile now, but I can't figure it out. Any ideas, or should I use a "real" programming language?
Here's one approach:
1. Build a list with 20 list elements consisting of the numbers from 1 to 20 in order.
2. Shuffle the list. You can do this by iterating through the list and swapping each list element with the list element at some other random location in the list.
3. Now, you can just draw numbers off the list in order to produce a list of random numbers that won't repeat.
Hope that helps.
Offline