Implementation code of php shuffling algorithm

WBOY
Release: 2016-07-25 08:58:30
Original
1129 people have browsed it
This article introduces an example of the code of the shuffling algorithm implemented in PHP. Friends in need can study and study it.

The code is as follows:

<?php
/**
* 简单洗牌算法
* edit bbs.it-home.org
* at 2013/6/19
*/
$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;
}
?>
Copy after login

This is a very simple piece of code, mainly to help you understand the shuffling algorithm. I hope it will be helpful to you.



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template