随机范围在PHP中没有重复

its*_*sMe 1 html php random

我有60张图片,但我想从1到60随机显示20张.

我的代码是这样的,它显示60

<?php
  for( $i = 1; $i < 61; $i++ ) 
    { 
       print '<a href="javascript:;"><img src="images/items/' . $i . '.png"  class="allitems item' . $i . '" /></a>'; 
    }    
?>
Run Code Online (Sandbox Code Playgroud)

我发现PHP函数RAND(),但无法实现,任何帮助将不胜感激.

Bli*_*itZ 11

尝试功能range()array_rand():

<?php
// range generates array with direct sequence from 1 to 60 (inclusive).
// array_rand extracts 20 random keys from it.
$range = array_rand(range(1, 60), 20);

while(count($range)){
    $i = array_shift($range) + 1;

    print '<a href="javascript:;"><img src="images/items/' . $i . '.png"  class="allitems item' . $i . '" /></a>';
}
?>
Run Code Online (Sandbox Code Playgroud)

UPDv1:使用for-loop:

<?php
$range = array_rand(range(1, 60), 20);

for($i = 0; $i < 20; $i++){
    $image = $range[$i] + 1;

    print '<a href="javascript:;"><img src="images/items/' . $image . '.png"  class="allitems item' . $image . '" /></a>';
}

unset($range, $i, $image);
?>
Run Code Online (Sandbox Code Playgroud)

UPDv2:

我误读了array_rand()手册.它返回数组而不是元素.这是多用途版本(修复array_flip()):

<?php
header('Content-Type: text/plain');

$buffer = range(1, 60);
$buffer = array_flip($buffer);
$buffer = array_rand($buffer, 20);

foreach($buffer as $value){
    echo $value, PHP_EOL;
}
?>
Run Code Online (Sandbox Code Playgroud)

和快捷功能(负面安全,总计数安全):

<?php
header('Content-Type: text/plain');

function random_range($min, $max, $count){
    $count = abs((int)$count);

    if($min > $max){
        list($min, $max) = array($max, $min);
    }

    $uniques = abs($max - $min);

    if($count > $uniques)$count = $uniques;

    return array_rand(array_flip(range($min, $max)), $count);
}

foreach(random_range(1, 60, 20) as $value){
    echo $value, PHP_EOL;
}
?>
Run Code Online (Sandbox Code Playgroud)

对于那些需要非增长随机序列的人来说还有另一种方式.用这个:

<?php
header('Content-Type: text/plain');

function random_range($min, $max, $count){
    $count = abs((int)$count);

    if($min > $max){
        list($min, $max) = array($max, $min);
    }

    $uniques = abs($max - $min);

    if($count > $uniques)$count = $uniques;

    $result = array();
    $ready  = 0;

    while($ready < $count){
        $buffer = rand($min, $max);

        if(!in_array($buffer, $result)){
            $result[] = $buffer;
            $ready++;
        }
    }

    return $result;
}

foreach(random_range(1, 60, 20) as $value){
    echo $value, PHP_EOL;
}
?>
Run Code Online (Sandbox Code Playgroud)

UPDv3:

另一种方式,使用range()+ shuffle()+ array_slice():

<?php
header('Content-Type: text/plain');

function random_range($min, $max, $count){
    $count = abs((int)$count);

    if($min > $max){
        list($min, $max) = array($max, $min);
    }

    $uniques = abs($max - $min);

    if($count > $uniques)$count = $uniques;

    $result = range($min, $max);
    shuffle($result);

    return array_slice($result, 0, $count);
}

foreach(random_range(5, 20, 5) as $random){
    echo $random, ' ';
}
?>
Run Code Online (Sandbox Code Playgroud)

  • `foreach(array_rand(范围(1,60),20)为$ image)...`任何人? (3认同)