Home  >  Article  >  Backend Development  >  Implementation code of php shuffling algorithm

Implementation code of php shuffling algorithm

WBOY
WBOYOriginal
2016-07-25 08:58:301074browse
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;
}
?>

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



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