Home  >  Article  >  Backend Development  >  php实现简单洗牌算法_php技巧

php实现简单洗牌算法_php技巧

WBOY
WBOYOriginal
2016-05-17 09:00:37822browse

如下所示:

复制代码 代码如下:

 /**
  * 简单洗牌算法
  */

 $card_num=54; //牌数
 print_r(wash_card($card_num));

 function wash_card($card_num)
 {
     $cards=$tmp=array();
     for($i=0;$i         $tmp[$i]=$i;
     }

     for($i=0;$i         $index=rand(0,$card_num-$i-1);
         $cards[$i]=$tmp[$index];
         unset($tmp[$index]);
         $tmp=array_values($tmp);
     }
     return $cards;
 }
 ?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn