跑了1W次,洗牌逻辑耗时2.5019571781158,没有出现重复率,但是感觉奇奇怪怪的,先留着吧。回头再说 无 class demo{ private $dic = array('a', 'b', 'c', 'd'); // 花色 private $kings = array('w1', 'w2'); // 大小王 private $card_set = array(); // 扑克
跑了1W次,洗牌逻辑耗时2.5019571781158, 没有出现重复率,但是感觉奇奇怪怪的,先留着吧。回头再说
class demo
{
private $dic = array('a', 'b', 'c', 'd'); // 花色
private $kings = array('w1', 'w2'); // 大小王
private $card_set = array(); // 扑克牌
private $card_array = array(); // 洗好的牌
// 执行随机洗牌
public function getCardSet()
{
$this->createCard();
for($i = 0; $i < 54; $i++)
{
$this->getRandNum();
$k = $this->key;
$this->card_array[$i] = $this->card_set[$k];
unset($this->card_set[$k]);
}
ksort($this->card_array);
return implode(',', $this->card_array);
}
// 生成扑克牌
public function createCard()
{
$tmp_card = array();
for($i = 1; $i <= 13; $i++)
{
$count = count($this->dic);
for($j = 0; $j < $count; $j++)
{
$tmp_card[] = $this->dic[$j] . $i;
}
}
$this->card_set = array_merge($tmp_card, $this->kings);
}
// 生成随机数
public function getRandNum()
{
$num = microtime(true);
$k = rand(0, 53);
if(! isset($this->card_set[$k]))
{
$this->getRandNum();
}
else
{
$this->key = $k;
}
}
}
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号