I want to generate a seed using CSPRNG. I then want to shuffle a standard pack of playing cards (52) using said seed, however, I want the shuffle to be accurate enough that it covers all the possible shuffle outcomes which is 52 factorial. I read from a blog post online that I need at least 226 bits of entropy to cover this. Here is the post for more information.

How would I go about implementing a CSPRNG seed that can shuffle a deck of cards in the same pattern each time given that the seed is the same? I am happy enough to take pseudo code as an explanation, or NodeJS/TypeScript code if possible.

Any help would be greatly appreciated, I'd like to understand exactly how this is achieved. If it is possible, and would still be considered a CSPRNG?

  • 3
    $\begingroup$ See, en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle , you need DRGB and seed it with CSPRNG. You, however, need to store the seed for your repeating purposes. $\endgroup$
    – kelalaka
    Commented Sep 30, 2022 at 21:48
  • $\begingroup$ Yeah, Fisher-Yates is the general idea. Another way similar to that is to randomly choose an integer from $0$ to $52!-1$, mod it by 52 to get first Fisher-Yates choice, divide by 52 rounding down, mod by 51 to get second Fisher-Yates choice... For choosing the random number, there are many ways using a CSPRNG. The simplest is to generate a 226-bit random string using methods kelalaka linked (DRGB), interpret as an integer, and retry if $r \ge 52!-1$. This will eventually succeed in practice, though theoretically could take forever. $\endgroup$
    – Myria
    Commented Sep 30, 2022 at 21:55
  • $\begingroup$ Oh, and another way is to double the number of bits and "mod": select a 452-bit random string and mod it by $52!$. This has bias, but the bias will be very small by doubling the number of input bits. Depending on your security requirements, this may be fine. RFC 4226 is an example where a mod is used because the bias is sufficiently small as to still be within security bounds. $\endgroup$
    – Myria
    Commented Sep 30, 2022 at 21:59
  • $\begingroup$ Are you absolutely sure you need it to cover all shuffle outcomes? If no one will ever be able to tell which shuffle outcomes are impossible, what would you lose by using a 128-bit seed? Here is javascript code to do it: crypto.stackexchange.com/questions/98841/… $\endgroup$
    – knaccc
    Commented Sep 30, 2022 at 22:52
  • 2
    $\begingroup$ Hiya & welcome! Struggling to understand the question though. Is the answer not your link (AESCounterRNG) + Fisher Yates? And if it's a casino situation, why would you want to exactly repeat the shuffle? Casino's can't. $\endgroup$
    – Paul Uszak
    Commented Oct 1, 2022 at 1:00


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.