gpt4 book ai didi

PHP 的 array_rand() 不是真正随机的?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:44:36 24 4
gpt4 key购买 nike

我正在编写一些代码来随机化演示文稿列表。同一演示文稿无法连续播放。

$d 数组将是演示文稿 ID 的列表以及演示文稿应循环播放的次数。

下面的代码工作正常,但结果并不是真正随机的。当我开始一遍又一遍地看到相同的模式时,我正在调试。看看这个输出:

ighbajafpbailgjacbiaeldiqjaphafgdjcbapsaebjfdkcknijhbdgecaimabodalkfbgbhacbhnrjeofbdjbfhegmbpdkialmbocnliaebfaimcabchgoecbcdimgepnfjgfbfbohdahdkjgneaebha

ighbajafpbailgjacbiaeldiqjaphafgdjcbapsaebjfdkcknijhbdgecaimabodalkfbgbhacbhnrjeofbdjbfhegmbpdkialmbocnliaebfaimcabchgoecbcdimgepnfjgfbfbohdahdkjgneaebha

这两个相差 4 分。它们是相同的。 4 运行后,相同的相同字符串。再运行 4 次,相同的字符串。事实上,当我运行它时,我似乎按顺序得到了相同的 4 个字符串。

我的代码在下面,谁能指出我可能犯了什么错误?如果我什么都没做,为什么“rand”不是随机的?我该如何解决这个问题?

$d = array(
array('a', '20'), array('b', '20'), array('c', '10'), array('d', '10'), array('e', '10'), array('f', '10'), array('g', '10'), array('h', '10'), array('i', '10'), array('j', '10'), array('k', '5'), array('l', '5'), array('m', '5'), array('n', '5'), array('o', '5'), array('p', '5'), array('q', '1'), array('r', '1'), array('s', '1'));
$out = randomizeArray($d);
//var_dump($out);

function randomizeArray ($data) {
// Step 1: Make an array of the id's with repeats for count.
$rarray = array();
foreach ($data as $i => $v) {
while ($data[$i][1] > 0) {
array_push($rarray, $data[$i][0]);
$data[$i][1]--;
}
}
// Step 2: Randomize.
$stat = 1;
while ($stat == '1') {
//echo "<br><br>Randomizing Array: <br>";
$stat = rarr($rarray);
}
return $stat;
}
function rarr ($a) {
$r = array();
$last = "";
while ($a) {
$rand = array_rand($a, 1);
// Does this value match the last one we got?
if ($a[$rand] != $last) {
// Nope. Transfer to our $r array.
echo $a[$rand];
array_push($r, $a[$rand]);
$last = $a[$rand];
unset($a[$rand]);
} else {
// We have a match between our "last" presentation and the one we just selected.
// We need to scan our array and verify that we still have different values.
$check = $a[$rand];
foreach ($a as $c) {
if ($check != $c) {
// We have at least 2 different values in our array still.
// Rerun the while loop
continue 2;
}
}
// All of the values left in our array are repeats.
// That's no good, so return an error and rerun.
return 1;
}
}
// Everything is awesome. Return the randomized array.
return $r;
}

最佳答案

据我所知,解决方案是在第 2 步之前添加行:

shuffle($rarray);

看来问题有点解决了。重复精确到 47 次。

如果相反,您在 rarr 函数中的 while 之后添加它:

while ($a) {
shuffle($a);

if 会重复 52 次

看来这实际上是 array_rand 函数的问题。在下面的代码中将 array_rand 更改为 mt_rand:

<?php
$d = array(
array('a', '20'), array('b', '20'), array('c', '10'), array('d', '10'), array('e', '10'), array('f', '10'), array('g', '10'), array('h', '10'), array('i', '10'), array('j', '10'), array('k', '5'), array('l', '5'), array('m', '5'), array('n', '5'), array('o', '5'), array('p', '5'), array('q', '1'), array('r', '1'), array('s', '1'));

for ($i=0; $i<10000; ++$i) {
//ho "x";
$out = randomizeArray($d);
echo implode('',$out)."<br />";
}


//var_dump($out);

function randomizeArray ($data) {
// Step 1: Make an array of the id's with repeats for count.
$rarray = array();
foreach ($data as $i => $v) {
while ($data[$i][1] > 0) {
array_push($rarray, $data[$i][0]);
$data[$i][1]--;
}
}
// Step 2: Randomize.
$stat = 1;
while ($stat == '1') {
//echo "<br><br>Randomizing Array: <br>";
$stat = rarr($rarray);
}
return $stat;
}
function rarr ($a) {
$r = array();
$last = "";
while ($a) {
$rand = mt_rand(0,count($a)-1);
// Does this value match the last one we got?
if ($a[$rand] != $last) {
// Nope. Transfer to our $r array.
// echo $a[$rand];
array_push($r, $a[$rand]);
$last = $a[$rand];
unset($a[$rand]);
$a = array_values($a);
} else {
// We have a match between our "last" presentation and the one we just selected.
// We need to scan our array and verify that we still have different values.
$check = $a[$rand];
foreach ($a as $c) {
if ($check != $c) {
// We have at least 2 different values in our array still.
// Rerun the while loop
continue 2;
}
}
// All of the values left in our array are repeats.
// That's no good, so return an error and rerun.
return 1;
}
}
// Everything is awesome. Return the randomized array.
return $r;
}

使字符串真正独一无二。

我真的很好奇是否有人可以解释这是一个 PHP 错误或“功能”

关于PHP 的 array_rand() 不是真正随机的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23522143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com