Home >Backend Development >PHP Tutorial >Implementation code of php shuffling algorithm
This article introduces an example of the code of the shuffling algorithm implemented in PHP. Friends in need can study and study it.
The code is as follows: <?php /** * 简单洗牌算法 * edit bbs.it-home.org * at 2013/6/19 */ $card_num=54; //牌数 print_r(wash_card($card_num)); function wash_card($card_num) { $cards=$tmp=array(); for($i=0;$i<$card_num;$i++){ $tmp[$i]=$i; } for($i=0;$i<$card_num;$i++){ $index=rand(0,$card_num-$i-1); $cards[$i]=$tmp[$index]; unset($tmp[$index]); $tmp=array_values($tmp); } return $cards; } ?> This is a very simple piece of code, mainly to help you understand the shuffling algorithm. I hope it will be helpful to you. |