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