in reply to How to not print the same random number
To make the concept clear, let me compare to a deck of cards. How do you prevent from drawing the same card twice? By not putting the card back in the deck before you draw another one.
You get the same result by simply shuffling the deck, and picking the next card, one by one.
shuffle is a function you can find in List::Util. You use it like this:
I get, for example, the following output:use List::Util 'shuffle'; my @shuffled_deck = shuffle 1 .. 10; local $\ = "\n"; foreach(@shuffled_deck) { print; }
1 2 9 8 7 10 4 5 6 3
It'll be far more efficient than retrying until you get something you haven't seen before, especially near the end, when you've almost seen them all.
|
|---|