Home >php教程 >php手册 >php 简单洗牌算法

php 简单洗牌算法

WBOY
WBOYOriginal
2016-06-13 09:42:281024browse

来源: http://phpman.cn/?p=37 
  标签:  

代码片段(1)

[代码] [PHP]代码

view source print? 01 <?php 02 /** 03  * @author     phpman.cn 04  * @date       2012/12/04 05  * @todo       简单洗牌算法 06  */ 07   08 <code class="variable">$card_num=54; //牌数 09 print_r(wash_card(<code class="variable">$card_num)); 10   11 function wash_card(<code class="variable">$card_num) 12 { 13     $cards=$tmp=array(); 14     for($i=0;$i<code class="variable">$card_num;$i++){ 15         $tmp[$i]=$i; 16     } 17   18     for($i=0;$i<code class="variable">$card_num;$i++){ 19         $index=rand(0,<code class="variable">$card_num-$i-1); 20         $cards[$i]=$tmp[$index]; 21         unset($tmp[$index]); 22         $tmp=array_values($tmp); 23     } 24     return $cards; 25 } 26 ?>
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