 <p>If you accept to recycle the random numbers, why do you want to wait for the exhaustion of the combinations before recycling?</p> <ul> <li>This makes the numbers less and less random when going to the end of the combination set</li> <li>This forces you to maintain some database to know which numbers have already been used and which haven't.</li> </ul> <p>I would just generate random numbers, without caring if they've been used already.</p> <p>If you really really want to keep it like you asked, here's how you could do it:</p> <ul> <li>Generate all the combinations and put them into a database table</li> <li>Store the size of this table in some variable</li> <li>Generate a random number R between 1 and the size of the table</li> <li>Fetch the combination stored at the Rth row of the table</li> <li>Delete the Rth row from the table, and decrement the size variable</li> <li>when the table is empty (and the size variable is 0), start again</li> </ul> <p>You could improve it by moving the used numbers from one table to another, and use the second table instead of the first one when the first table is empty.</p> <p>You could also do that in memory, if you have enough of it.</p>