Five unique, random numbers from a subset

Posted by tau on Stack Overflow See other posts from Stack Overflow or by tau
Published on 2010-06-09T05:02:55Z Indexed on 2010/06/09 5:22 UTC
Read the original article Hit count: 252

I know similar questions come up a lot and there's probably no definitive answer, but I want to generate five unique random numbers from a subset of numbers that is potentially infinite (maybe 0-20, or 0-1,000,000).
The only catch is that I don't want to have to run while loops or fill an array.

My current method is to simply generate five random numbers from a subset minus the last five numbers. If any of the numbers match each other, then they go to their respective place at the end of the subset. So if the fourth number matches any other number, it will bet set to the 4th from the last number.

Does anyone have a method that is "random enough" and doesn't involve costly loops or arrays?

Please keep in mind this a curiosity, not some mission-critical problem. I would appreciate it if everyone didn't post "why are you having this problem?" answers. I am just looking for ideas.
Thanks a lot!

© Stack Overflow or respective owner

Related posts about php

Related posts about algorithm