php 在PHP中生成随机唯一数字数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10824770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:07:45  来源:igfitidea点击:

Generate array of random unique numbers in PHP

php

提问by Phil

I'm trying to generate an array of random numbers from 0-n then shuffle (but ensure that the keys and values DO NOT match).

我正在尝试从 0-n 生成一个随机数数组,然后进行洗牌(但要确保键和值不匹配)。

For example:

例如:

0 => 3
1 => 2
2 => 4
3 => 0
4 => 1

Note that both keys and values are from 0-4 but none of the keys and values are the same.

请注意,键和值都在 0-4 之间,但没有一个键和值是相同的。

Any thoughts?

有什么想法吗?

采纳答案by AMayer

$max = 5;
$done = false;
while(!$done){
    $numbers = range(0, $max);
    shuffle($numbers);
    $done = true;
    foreach($numbers as $key => $val){
        if($key == $val){
            $done = false;
            break;
        }
    }
}

回答by stonyau

A even shorter solution:

一个更短的解决方案:

$random_number_array = range(0, 100);
shuffle($random_number_array );
$random_number_array = array_slice($random_number_array ,0,10);

print_r($random_number_array);

Result will be:

结果将是:

[0] => 53
[1] => 6
[2] => 16
[3] => 59
[4] => 8
[5] => 18
[6] => 62
[7] => 39
[8] => 22
[9] => 26

回答by ziad-saab

Naive solution:

天真的解决方案:

$n = 10;
$rands = array();
for($i=0; $i<$n;$i++) {
  $ok = false;
  while(!$ok) {
    $x=mt_rand(0,$n-1);
    $ok = !in_array($x, $rands) && $x != $i;
  }
  $rands[$i]=$x;
}

var_dump($rands);

Efficient solution:

有效的解决方案:

$n = 100;  
$numbers = range(0, $n-1);
$rands = array();
for ($i=0; $i < $n; $i++) {
  $ok = false;
  while (!$ok) {
    $x = array_rand($numbers);
    $ok = !in_array($numbers[$x], $rands) && $numbers[$x] != $i;
  }
  $rands[$i] = $numbers[$x];
  unset($numbers[$x]);
}

var_dump($rands);

edit:s/rand/mt_rand/

编辑:s/rand/mt_rand/

edit #2:both solutions can end up in a deadlock, as mentioned by @AMayer. I stand corrected.

编辑#2:正如@AMayer 所提到的,这两种解决方案都可能陷入僵局。我站着纠正。

回答by kba

Here's a rather long, but also pretty efficient solution, I believe. Contrary to other solutions posted here, this cannot deadlock (unless $size<2), and this will not do a full shuffle every time one value doesn't fit. Instead, it will only replace that value with another, random value.

我相信这是一个相当长但也非常有效的解决方案。与此处发布的其他解决方案相反,这不会死锁(除非$size<2),并且每次一个值不合适时都不会进行完全洗牌。相反,它只会用另一个随机值替换该值。

function unique_list($size=5) {

    function all_unique($numbers) {
        foreach ($numbers as $key=>$value)
            if ($key==$value) return false;
        return true;
    }
    function flip($a, $b, &$numbers) {
        $numbers[$a] = $numbers[$a] + $numbers[$b];
        $numbers[$b] = $numbers[$a] - $numbers[$b];
        $numbers[$a] = $numbers[$a] - $numbers[$b];
    }

    $flip_count = 0;
    $numbers = range(0,$size-1);
    shuffle($numbers);

    while (!all_unique($numbers)) {
        foreach ($numbers as $key=>$value) {
            if ($key==$value) {
                flip($key, rand(0,$size-1), $numbers);
                $flip_count++;
                break;
            }
        }
    }

    printf("Flipped %d values\n", $flip_count);
    return $numbers;

}

$list = unique_list(10);
print_r($list);

The above will print something similar to

以上将打印类似于

Flipped 1 value(s)
Array
(
    [0] => 2
    [1] => 5
    [2] => 7
    [3] => 9
    [4] => 6
    [5] => 3
    [6] => 1
    [7] => 8
    [8] => 0
    [9] => 4
)