【问题标题】:Random range without duplicates in PHPPHP中没有重复的随机范围
【发布时间】:2013-04-17 12:56:39
【问题描述】:

我有 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>'; 
    }    
?>

我找到了 PHP 函数 RAND(),但无法实现,不胜感激。

【问题讨论】:

标签: php html random


【解决方案1】:

试试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>';
}
?>

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

UPDv2:

我误读了array_rand()s 手册。它返回数组 keys 而不是 elements。 这是多用途版本(使用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;
}
?>

还有一个快捷功能(负数安全,总计数安全):

<?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;
}
?>

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

<?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;
}
?>

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, ' ';
}
?>

【讨论】:

  • 一个不错的有效答案。 ^^
  • 我会使用常规循环而不是 while..count..array_shift...
  • @deceze 我会解决的......这很有意义。
  • 第一个没有.. 更新,嗯,只选择前 20 张图像。哈哈。 =P 编辑他现在修复了更新和第二部分^^
  • foreach (array_rand(range(1, 60), 20) as $image) ... 有人吗?
猜你喜欢
  • 2017-02-13
  • 2012-10-10
  • 1970-01-01
  • 2011-02-27
  • 1970-01-01
  • 2018-05-15
  • 1970-01-01
  • 2020-01-05
  • 2014-07-29
相关资源
最近更新 更多