Re: [PHP] shuffle or mt_rand
On Fri, June 29, 2007 4:57 am, Ryan A wrote: > Have a quick question about which is better to get real random values, > shuffle or mt_rand. I *thought* that shuffle used mt_rand under the hood, but could be totally wrong... As far as REAL random values goes, that's an encyclopedia of information, really... Unless there's real money on the table, shuffle or mt_rand should be fine. > 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(). > Any idea which would be better to constantly generate proper random > numbers for a long period of time? I'd have to go with Tijnema's code because it will be maintainable long-term MUCH easier, and I doubt that either is significantly "more" random than the other. -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] shuffle or mt_rand
On 6/29/07, Ryan A <[EMAIL PROTECTED]> wrote: Just 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 1: Ryan's code:1 Tijnema's code:1 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 1 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 1 random decks: Microtime difference: Ryan's code:0.82403707504272 Tijnema's code:0.40426802635193 Unique decks out of 1: Ryan's code:1 Tijnema's code:1 >>> Hey Tijnema, I really had no doubt about your code being faster and cleaner... (as i mentioned in my original post) but can you send me the benchmarking scripts that you used so i may test them a bit more? Thanks! Ryan Sure, you can look at the code here: http://86.86.80.41/cards.phps Tijnema -- - The faulty interface lies between the chair and the keyboard. - Creativity is great, but plagiarism is faster! - Smile, everyone loves a moron. :-) Don't be flakey. Get Yahoo! Mail for Mobile and always stay connected to friends. -- Vote for PHP Color Coding in Gmail! -> http://gpcc.tijnema.info -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] shuffle or mt_rand
Just 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 1: Ryan's code:1 Tijnema's code:1 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 1 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 1 random decks: Microtime difference: Ryan's code:0.82403707504272 Tijnema's code:0.40426802635193 Unique decks out of 1: Ryan's code:1 Tijnema's code:1 >>> Hey Tijnema, I really had no doubt about your code being faster and cleaner... (as i mentioned in my original post) but can you send me the benchmarking scripts that you used so i may test them a bit more? Thanks! Ryan -- - The faulty interface lies between the chair and the keyboard. - Creativity is great, but plagiarism is faster! - Smile, everyone loves a moron. :-) - Don't be flakey. Get Yahoo! Mail for Mobile and always stay connected to friends.
Re: [PHP] shuffle or mt_rand
Just 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 1: Ryan's code:1 Tijnema's code:1 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 1 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 1 random decks: Microtime difference: Ryan's code:0.82403707504272 Tijnema's code:0.40426802635193 Unique decks out of 1: Ryan's code:1 Tijnema's code:1 Tijnema Just a pointer: Just because you get 1 different decks doesn't mean that it is random. Since there are 52! (around 8*10^67) different decks and you choose 1 of theese you would be very lucky to get 2 that are the same. -- /Thunis "I refuse to answer that question on the grounds that I don't know the answer." --The Hitchikers Guide to the Galaxy -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] shuffle or mt_rand
On 6/29/07, Ryan A <[EMAIL PROTECTED]> 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? Any comments and suggestions are also welcome. Thanks! Ryan Just 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 1: Ryan's code:1 Tijnema's code:1 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 1 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 1 random decks: Microtime difference: Ryan's code:0.82403707504272 Tijnema's code:0.40426802635193 Unique decks out of 1: Ryan's code:1 Tijnema's code:1 Tijnema -- Vote for PHP Color Coding in Gmail! -> http://gpcc.tijnema.info -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] shuffle or mt_rand
Ryan A 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(). Well they do different things - shuffle() randomises an array, whereas mt_rand() gives you a random number. If you want to randomise an array, use shuffle(). If you want a random number, use mt_rand(). -- Richard Heyes +44 (0)844 801 1072 http://www.websupportsolutions.co.uk Knowledge Base and HelpDesk software -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php