Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何配置php的“array_rand”,使其不连续给出两个相同的结果?_Php_Arrays_Random - Fatal编程技术网

如何配置php的“array_rand”,使其不连续给出两个相同的结果?

如何配置php的“array_rand”,使其不连续给出两个相同的结果?,php,arrays,random,Php,Arrays,Random,我使用php数组_rand从数组中选择1条随机记录,例如: $style_class = array("st_style1","st_style2","st_style3","st_style4"); $random_class = array_rand($style_class, 1); $div_class = $style_class[$random_class]; 问题是,有时它会多次给出同一条记录,而由于我只使用了4条记录,所以经常使用数组并不必要 例如: st_style1, st

我使用php数组_rand从数组中选择1条随机记录,例如:

$style_class = array("st_style1","st_style2","st_style3","st_style4");
$random_class = array_rand($style_class, 1);
$div_class = $style_class[$random_class];
问题是,有时它会多次给出同一条记录,而由于我只使用了4条记录,所以经常使用数组并不必要

例如:

st_style1, st_style2, st_style2, st_style2, st_style4, st_style2

有没有办法解决这个问题,使两条相同的记录不会连续显示两次

比如说


st_style2,st_style4,st_style2,st_style1,st_style3,st_style2,st_style1…

将最后一个样式保存在变量中,然后循环,直到新样式与最后一个样式不同。然后每次执行时都会出现与上次不同的结果。

最简单的解决方案是跟踪最新的结果,并一直随机调用,直到得到不同的结果。比如:

$style_class = array("st_style1","st_style2","st_style3","st_style4");
$styles = array()
$lastStyle = -1
for($i = 0; $i < 5; $i++) {
    while(1==1) {
        $newStyle = array_rand($style_class, 1);
        if ($lastStyle != $newStyle) { 
            $lastStyle = $newStyle;
            break;
        }
    }
    $div_class = $style_class[$lastStyle]
    $styles[] = $div_class
}
然后依次使用$styles[]数组。它不应该有任何与基本相同的副本,但使用的是:

按如下方式设置阵列:

$style_class = array("st_style1","st_style2","st_style3","st_style4");
$prev_class = -1;
然后,为了获得一个随机类:

do {
    $random_class = array_rand($style_class, 1);
} while ($random_class == $prev_class);
$div_class = $style_class[$prev_class = $random_class];
$random_class += rand(1, count($style_class)-1);
$div_class = $style_class[$random_class % count($style_class)];
编辑:替代解决方案,不带循环:

$style_class = array("st_style1","st_style2","st_style3","st_style4");
$random_class = array_rand($style_class);
要获取新的随机类,请执行以下操作:

do {
    $random_class = array_rand($style_class, 1);
} while ($random_class == $prev_class);
$div_class = $style_class[$prev_class = $random_class];
$random_class += rand(1, count($style_class)-1);
$div_class = $style_class[$random_class % count($style_class)];

只要数组键是从零开始的连续整数,这一点就有效,如果使用数组定义它,并且不显式指定任何键,则情况就是如此。

重复序列是随机性的一部分。这是极不可能的,但你可以在掷硬币时连续10亿次得到人头。看起来不是随机的,但这是可能的。因此,在这种情况下,如果我使用正确的代码,我会使用$lastStyle而不是$div_类?尝试过,似乎有效,但有时我会得到两次甚至更多相同的记录:/-我使用的代码-$style_类=arrayst_style1,st_style2,st_style3,st_style4;$lastStyle=-1;while1==1{$newStyle=array_rand$style_class,1;if$lastStyle!=$newStyle{$lastStyle=$newStyle;break;}}$div_class=$style_class[$lastStyle];这难道不违背“随机”的概念吗?我再次编辑它以显示完整的代码。运行此操作后,您将获得所需的所有样式。只需将5更改为您需要的数量。