Is there a difference between the 2 below methods for choosing a random card from a deck of cards? Is one approach more accepted than the other?
Method 1: Generate a random number from 1-52 for the first card them remove that card from the array and choose a number from 1-51, then 1-50, then 1-49, etc... to get subsequent cards from the deck.
Method 2: Generate a number from 1-52 and add that number to a “used numbers array”. For each additional card, continue to choose a number from 1-52 each time, then check the used numbers array to see if it’s already been used. Repeat generating a number between 1-52 until you get a result that hasn’t been used, then add it to the used number array.
Obviously, method 1 is more efficient in terms of compute cycles - but is there any other difference between the 2 approaches? Is one approach “more random” than the other? Or “better” for any other reason related to the quality of the randomization?
sum from 0 .. n-1 of n/(n-k)
which is the same asn * Hn
where Hn is the nth harmonic number, which grows as O(log n). Overall, that's O(n log n) – Alex Reinking Mar 25 '19 at 08:48