php实现简单洗牌算法

yipeiwu_com7年前PHP代码库
如下所示:
复制代码 代码如下:

<?php
 /**
  * 简单洗牌算法
  */

 $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;
 }
 ?>

相关文章

PHP 图像尺寸调整代码

复制代码 代码如下: /********************** *@filename - path to the image *@tmpname - temporary path...

php程序效率优化的一些策略小结

1.在可以用file_get_contents替代file、fopen、feof、fgets等系列方法的情况下,尽量用 file_get_contents,因为他的效率高得多!但是要注意...

php读取html并截取字符串的简单代码

复制代码 代码如下:<?php $title='【宜配屋www.yipeiwu.com】'; $hello='jb51.net!'; $file=file_get_contents...

老生常谈PHP位运算的用途

在实际应用中可以做用户权限的应用 我这里说到的权限管理办法是一个普遍采用的方法,主要是使用到”位运行符”操作,& 位与运算符、| 位或运行符。参与运算的如果是10进制数,则会被转换至2进...

php实现根据字符串生成对应数组的方法

本文实例讲述了php实现根据字符串生成对应数组的方法,是比较实用的技巧。分享给大家供大家参考。具体方法如下: 先看看如下示例: <?php $config = arr...