3 ms·
That is essentially the same as the algorithm in the article, which was Figure 3: for (i is 1 to 52) Swap i with random position between i and 52
by rav 11y ago
That is essentially the same as the algorithm in the article, which was Figure 3:
for (i is 1 to 52)
Swap i with random position between i and 52
After i iterations, the first i entries are your "shuffled", and the last 52 - i entries are your "cards". "random.choice(cards)" corresponds to picking a "random position between i and 52".
- lmm 11y agoBut they didn't do that - they picked a random position between 1 (not i) and 52 each time, which gives a biased shuffle. Even if their randomness had been perfect, this would have been problematic.
- eximius 11y agoI suppose. I guess I just prefer the more direct, naive algorithm when the performance gain would seem to be so small. (not that I've profiled it or anything :/)
- rav 11y agoFrom a CS theory perspective, I would say it's two implementations of the same algorithm, or two ways of phrasing the same algorithm. The most natural formulation in your eyes is probably not the same as the most natural formulation in the eyes of the article author.