在PHP中没有重复的随机范围 [英] Random range without duplicates in PHP
问题描述
我有60张图片,但我想从1到60随机显示20个。
我的代码是这样的,它显示60
($ i = 1; $ i <61; $ i ++){$ b $
$ b pre $
<?php
b print'< a href =javascript:;>< img src =images / items /'。$ i。'.pngclass =allitems item'。$ i。'/>< ; / A>';
}
?>
我找到了PHP函数RAND(),但无法实现,任何帮助都不胜感激。 p>
尝试函数 range()
和 $ b
><?php
//范围生成从1到60(含)的直接序列的数组。
// array_rand从中提取20个随机密钥。
$ range = array_rand(range(1,60),20);
while(count($ range)){
$ i = array_shift($ range)+ 1;
$ b $ print'< a href =javascript:;>< img src =images / items /'。$ i。'.pngclass =allitems item'。$ i 。'/>< / a>';
}
?>
UPDv1: $ b
< / code> ?php
$ range = array_rand(range(1,60),20); ($ i = 0; $ i <20; $ i ++){
$ image = $ range [$ i] + 1;
$ b;
$ b $ print'< a href =javascript:;>< img src =images / items /'。$ image。'.pngclass =allitems item'。$ image 。'/>< / a>';
}
unset($ range,$ i,$ image);
?>
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为$ value){
echo $ value,PHP_EOL;
}
?>
以及一个快捷函数(负数安全,总数安全):
<?php
header('Content-Type:text / plain');
函数random_range($ min,$ max,$ count){
$ count = abs((int)$ count);
$ b $ 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');
函数random_range($ min,$ max,$ count){
$ count = abs((int)$ count);
$ b $ 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 ++;
}
}
返回$ result;
foreach(random_range(1,60,20)as $ value){
echo $ value,PHP_EOL;
}
?>
UPDv3:
另一种方式,使用 range()
+ shuffle()
+ $ b
><?php
header('Content-Type:text / plain');
函数random_range($ min,$ max,$ count){
$ count = abs((int)$ count);
$ b $ if($ min> $ max){
list($ min,$ max)= array($ max,$ min);
}
$ uniques = abs($ max - $ min);
if($ count> $ uniques)$ count = $ uniques;
$结果=范围($ min,$ max);
shuffle($ result);
返回array_slice($ result,0,$ count);
}
foreach(random_range(5,20,5)as $ random){
echo $ random,'';
}
?>
I have 60 images but I want to randomly show 20 from 1 to 60.
My code is something like this, it shows 60
<?php
for( $i = 1; $i < 61; $i++ )
{
print '<a href="javascript:;"><img src="images/items/' . $i . '.png" class="allitems item' . $i . '" /></a>';
}
?>
I found PHP function RAND(), but unable to implement, any help would be appreciated.
Try functions range()
and 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: With 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:
I've misread array_rand()
s manual. It returns array keys instead of elements.
Here is multipurpose version (fix with 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;
}
?>
And a shortcut function (negatives safe, and overall count safe):
<?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;
}
?>
There is another way for those, who needs non-growing random sequence. Use this:
<?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:
Another way, used 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, ' ';
}
?>
这篇关于在PHP中没有重复的随机范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!