Maison > Article > développement back-end > php 简单洗牌算法_PHP教程
来源: http://phpman.cn/?p=37
标签:
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
?>