Вопрос по php – Генерация массива случайных уникальных чисел в PHP

11

Я пытаюсь сгенерировать массив случайных чисел из 0-n, затем перемешать (но убедиться, что ключи и значения НЕ совпадают).

Например:

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

Обратите внимание, что ключи и значения находятся в диапазоне от 0 до 4, но ни один из ключей и значений не совпадает.

Какие-нибудь мысли?

Error: User Rate Limit Exceeded Steve Robbins
Error: User Rate Limit Exceeded Loïs Di Qual
Error: User Rate Limit Exceeded bumperbox

Ваш Ответ

4   ответа
4
$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;
        }
    }
}
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Phil
Error: User Rate Limit Exceeded Phil
Error: User Rate Limit Exceeded$rounds = 1;Error: User Rate Limit Exceededif($rounds++ >= 10000){ break 2; }
12

Error: User Rate Limit Exceeded

$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);

Error: User Rate Limit Exceeded

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

Error: User Rate Limit Exceeded

$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);

Error: User Rate Limit Exceeded

$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:Error: User Rate Limit Exceeded

edit #2:Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededarray(1=>3, 2=>1, 3=>2)Error: User Rate Limit Exceeded$n=5Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Phil
Error: User Rate Limit Exceeded
2

Error: User Rate Limit Exceeded$size<2Error: User Rate Limit Exceeded

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);

Error: User Rate Limit Exceeded

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
)

Похожие вопросы