开发者

Is MySQL's RAND() good enough for "shuffling" card decks for card games?

开发者 https://www.devze.com 2023-02-12 13:02 出处:网络
I\'ve got a poker and b开发者_StackOverflow社区lackjack game that store a base card deck in a MySQL database. In order to shuffle the cards, I order the table at random using ORDER BY RAND() and inser

I've got a poker and b开发者_StackOverflow社区lackjack game that store a base card deck in a MySQL database. In order to shuffle the cards, I order the table at random using ORDER BY RAND() and insert the cards in that order into a different table. Will using RAND() result in realistic odds that would be found playing with real cards and physically shuffling, or is this function not random enough?


Shuffling with real cards isn't really all that random unless you practise it a lot. It's easy to shuffle incorrectly so that either the top or the bottom of the pack isn't shuffled well.

Using ORDER BY RAND() to shuffle is a reasonable approach but there are some things to be aware of:

  • A slight bias is introduced if two exactly equal random numbers are generated by RAND() as it will not shuffle those two cards correctly with respect to each other.
  • RAND() is not cryptographically secure. By seeing some of the first cards in the deck it might be possible for a skilled attacker to deduce the internal state of the PRNG used to shuffle the deck and therefore predict the remaining cards.
  • ORDER BY RAND() requires O(n log(n)) operations. It will most likely have acceptable performance for shuffling 52 rows, but it is probably not something you want to use for shuffling millions of rows, for example.

For entertainment purposes your approach should be fine. If this is for serious money you might want to use a better shuffle algorithm such as the Fisher Yates shuffle and cryptographically secure random number generator.


For your use: Probably.

From the gaming industry perspective: No.

The Nevada Gaming Commission oversees the odds and randomness of approved games here. For such a purpose, it is in the interest of the house to eliminate the possibility of RE the randomizer so the real gaming software uses expiring "true random" seeds which must be obtained by certified methods (usually using some naturally chaotic input). Banks will often employ similar tools.

For further reading, see LavaRand

http://www.lavarand.com/

0

精彩评论

暂无评论...
验证码 换一张
取 消