Re: shuffle or mt_rand
| From: | Tijnema | Date: | Fri, 29 Jun 2007 11:07:40 +0000 |
| Subject: | Re: shuffle or mt_rand | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-257951@lists.php.net to get a copy of this message | ||
On 6/29/07, Ryan A <genphp@yahoo.com> wrote:
Hey, Have a quick question about which is better to get real random values, shuffle or mt_rand. I am trying to shuffle a deck of cards and reading another thread I saw that after a couple of thousand random generations patterns have been observed using rand()... so its better to use mt_rand(). After i wrote a function (copied below) to get real random cards from a 52 deck, Tijnema from the list kindly sent me a piece of code thats simpler/faster/should or would use less processing and does exactly what my function does except it uses shuffle()... Any idea which would be better to constantly generate proper random numbers for a long period of time? <code>
Any comments and suggestions are also welcome. Thanks! RyanJust did a quick benchmark for 10.000 hands (making a full deck on your code, 23.5 players): Microtime difference: Ryan's code:15.725826978683 Tijnema's code:0.40006709098816 Unique decks out of 10000: Ryan's code:10000 Tijnema's code:10000 When making a full deck my code is 40 times faster, and a lot less memory intensive. And as you can see, for both all 10000 decks are unique, so both are random :) But, also when generating cards for only 4 players, my code is twice as fast as yours, and both generate still 10000 random decks: Microtime difference: Ryan's code:0.82403707504272 Tijnema's code:0.40426802635193 Unique decks out of 10000: Ryan's code:10000 Tijnema's code:10000 Tijnema -- Vote for PHP Color Coding in Gmail! -> http://gpcc.tijnema.info