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
[PHP] shuffle or mt_rand
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? = My original code with mt_rand() function load_cards($no_of_players) { $i=0; $cards=array(); while($i<1) { if(empty($cards[0])) { $cards[]=mt_rand(1,52); } else { $no_of_cards_already_in_array=count($cards); $cards_to_be_dealt=$no_of_players * 2; $cards_to_be_dealt=$cards_to_be_dealt + 5; if($no_of_cards_already_in_array < $cards_to_be_dealt) { $new_generated_card=mt_rand(1,52); if(!in_array($new_generated_card,$cards)) { $cards[]=$new_generated_card; } }else { // end the loop $i=2; break; // just in case the above does not break outa the loop. } } } return $cards; } == Contributed code === // only generating 5 numbers here $deck=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52); shuffle($deck); for($i=0; $i<5; $i++) { $card = array_pop($deck); echo $card.""; } == Any comments and suggestions are also welcome. Thanks! Ryan -- - The faulty interface lies between the chair and the keyboard. - Creativity is great, but plagiarism is faster! - Smile, everyone loves a moron. :-) - Need a vacation? Get great deals to amazing places on Yahoo! Travel.
Re: [PHP] shuffle
* Thus wrote Edward Peloke: > it is strange... > > if I do this: > > function nextRecord(){ > $this->[EMAIL PROTECTED]($this->queryID); Don't use the @ sign, it is bad practice. > $test=$this->record; > echo $test; I'm not sure what this is for. > > $status=is_array($this->record); > return ($status); > } > > "Array" is echoed for $test so I know it is an array but if I add this: > > shuffle($test); Where in the world did $test get set from? if $test is expected to be the value of $this->record, then all your going to be doing is shuffling the order of the columns. Curt -- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] shuffle
I didn't try that but it works great...thanks! -Original Message- From: Torsten Roehr [mailto:[EMAIL PROTECTED] Sent: Wednesday, July 14, 2004 3:53 PM To: [EMAIL PROTECTED] Subject: Re: [PHP] shuffle "Edward Peloke" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > it is strange... > > if I do this: > > function nextRecord(){ > $this->[EMAIL PROTECTED]($this->queryID); > $test=$this->record; > echo $test; > > $status=is_array($this->record); > return ($status); > } > > "Array" is echoed for $test so I know it is an array but if I add this: > > shuffle($test); > > I get the warning that shuffle() expects parameter 1 to be an array, boolean > given. Have you tried shuffling in MySQL?: SELECT * FROM table ORDER BY RAND() Regards, Torsten -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] shuffle
"Edward Peloke" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > it is strange... > > if I do this: > > function nextRecord(){ > $this->[EMAIL PROTECTED]($this->queryID); > $test=$this->record; > echo $test; > > $status=is_array($this->record); > return ($status); > } > > "Array" is echoed for $test so I know it is an array but if I add this: > > shuffle($test); > > I get the warning that shuffle() expects parameter 1 to be an array, boolean > given. Have you tried shuffling in MySQL?: SELECT * FROM table ORDER BY RAND() Regards, Torsten -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] shuffle
it is strange... if I do this: function nextRecord(){ $this->[EMAIL PROTECTED]($this->queryID); $test=$this->record; echo $test; $status=is_array($this->record); return ($status); } "Array" is echoed for $test so I know it is an array but if I add this: shuffle($test); I get the warning that shuffle() expects parameter 1 to be an array, boolean given. -Original Message- From: Edward Peloke [mailto:[EMAIL PROTECTED] Sent: Wednesday, July 14, 2004 2:23 PM To: Php-General Subject: [PHP] shuffle I am using a db class which has the following method for cycling through the records function nextRecord(){ $this->[EMAIL PROTECTED]($this->queryID); $status=is_array($this->Record); return ($status); } where queryID is set by running the query. It works fine and I can run it as follows $sql->query("select * from clients"); while ($sql->nextRecord()){ echo $sql->getField("name");} Problem is, I want to have the array shuffled so the rows are randomly displayed each time the user hits the page. I have tried shuffle around the @mysql_fetch_array call and $this->queryID but nothing seems to work...any ideas? Thanks, Eddie WARNING: The information contained in this message and any attachments is intended only for the use of the individual or entity to which it is addressed. This message may contain information that is privileged, confidential and exempt from disclosure under applicable law. It may also contain trade secrets and other proprietary information for which you and your employer may be held liable for disclosing. You are hereby notified that any unauthorized dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify [EMAIL PROTECTED] by E-Mail and then destroy this communication in a manner appropriate for privileged information. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] shuffle
I am using a db class which has the following method for cycling through the records function nextRecord(){ $this->[EMAIL PROTECTED]($this->queryID); $status=is_array($this->Record); return ($status); } where queryID is set by running the query. It works fine and I can run it as follows $sql->query("select * from clients"); while ($sql->nextRecord()){ echo $sql->getField("name");} Problem is, I want to have the array shuffled so the rows are randomly displayed each time the user hits the page. I have tried shuffle around the @mysql_fetch_array call and $this->queryID but nothing seems to work...any ideas? Thanks, Eddie WARNING: The information contained in this message and any attachments is intended only for the use of the individual or entity to which it is addressed. This message may contain information that is privileged, confidential and exempt from disclosure under applicable law. It may also contain trade secrets and other proprietary information for which you and your employer may be held liable for disclosing. You are hereby notified that any unauthorized dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify [EMAIL PROTECTED] by E-Mail and then destroy this communication in a manner appropriate for privileged information. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Shuffle Cards Function
I've written a ShuffleCards function that will actually shuffle a deck of cards just like would happen in real life (divide the deck in half and stick them into each other with 1-3 cards between each other card) function ShuffleCards(&$cardsArray, $times) { // Randomizes where to split within center (-3 to +3 from dead center) (MINIMUM 10 CARDS IN DECK) // Splits into two array. Randomizes how many cards from left and how many cards from right (between 1 and 3) // Alternates sides. Continues until both arrays are empty. $arraySize = count($cardsArray); if($arraySize<10) return; $deadCenter = $arraySize/2; for($i=0;$i<$times;$i++) { reset($cardsArray); $cutVariant = rand(-3, 3); $cutLocation = $deadCenter+$cutVariant; $firstArray = array(); $secondArray = array(); for($z=0;$z<$arraySize;$z++) { if($z<$cutLocation) array_push($firstArray, $cardsArray[$z]); else array_push($secondArray, $cardsArray[$z]); } $bottomFirst = rand(0, 1); $cardsArray = array(); while(count($firstArray) && count($secondArray)) { $leftNewCards = array(); $rightNewCards = array(); $leftVariant = rand(1, 3); if($leftVariant>count($firstArray)) $leftVariant = count($firstArray); $rightVariant = rand(1, 3); if($rightVariant>count($secondArray)) $rightVariant = count($secondArray); for($x=0;$x<$leftVariant;$x++) { array_push($leftNewCards, array_shift($firstArray)); } for($y=0;$y<$rightVariant;$y++) { array_push($rightNewCards, array_shift($secondArray)); } if($bottomFirst==0) { $newCardsArray = array_merge($leftNewCards, $rightNewCards); $bottomFirst = 1; } else { $newCardsArray = array_merge($rightNewCards, $leftNewCards); $bottomFirst = 0; } reset($newCardsArray); while(count($newCardsArray)) { array_push($cardsArray, array_shift($newCardsArray)); } } if(count($firstArray)) { while(count($firstArray)) array_push($cardsArray, array_shift($firstArray)); } if(count($secondArray)) { while(count($secondArray)) array_push($cardsArray, array_shift($secondArray)); } } } Robert Schultz - [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]