I know how to select one random item from an array, but how about ten random items from an array of, say, twenty items? (In PHP.)
我知道如何从一个数组中选择一个随机项,但是如果从一个数组中选择10个随机项,比如20个,会怎么样呢?(PHP)。
What makes it a little more complicated is that each item actually has two parts: a filename, and a description. Basically, it's for a webpage that will display ten random images each time you reload. The actual format of this data doesn't really matter, although it's simple enough that I'd prefer to contain it in flat-text or even hard code it rather than set up a database. (It's also not meant to change often.)
更复杂的是,每个条目实际上有两个部分:一个文件名和一个描述。基本上,它是一个网页,每次你重新加载时都会显示10张随机的图片。这个数据的实际格式并不重要,尽管它非常简单,我宁愿将它包含在平面文本中,甚至是硬编码中,也不愿设置数据库。(它也不意味着经常改变。)
Bonus question, not sure if I'm going to do this just yet - but how would you weight the entries, such that certain items always get picked, or at least more frequently than others?
额外的问题,我不确定我是否要这么做——但是你会如何权衡条目,以便某些条目总是被选中,或者至少比其他条目更频繁?
Thanks.
谢谢。
7 个解决方案
#1
16
How to select one or more random items from an array in php: http://us3.php.net/manual/en/function.array-rand.php
如何从php数组中选择一个或多个随机项:http://us3.php.net/manual/en/function.array-rand.php
How to do random weighted elements:
http://20bits.com/articles/random-weighted-elements-in-php/
如何做随机加权元素:http://20bits.com/articles/random-weighted- elements-inphp/
#2
21
You could shuffle
the array and then pick the first ten elements with array_slice
:
你可以洗牌数组,然后用array_slice选择前10个元素:
shuffle($array);
$tenRandomElements = array_slice($array, 0, 10);
#3
3
Example #1 array_rand() example
例# 1()用于示例
<?php
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
?>
#4
0
An array of arrays in PHP should be a good strategy. You can keep the data for these array in any way you like (hard-coded, XML, etc) and arrange it in the arrays as such:
PHP中的数组应该是一种很好的策略。您可以以任何您喜欢的方式(硬编码、XML等)保存这些数组的数据,并将其按如下方式排列:
Array {
Array (item0) { filename,description, weight,...}
Array (item1) { filename,description, weight,...}
Array (item2) { filename,description, weight,...}
}
You can then use the array_rand function to randomly remove items from the array. Creating a weight value for each entry will allow you to pick one entry over another using some sort of priority strategy (e.g. randomly get 2 items from the array, check weight, pick the one with a greater weight and replace the other)
然后可以使用array_rand函数从数组中随机删除项。为每个条目创建一个权重值将允许您使用某种优先策略(例如,从数组中随机获取两个条目,检查权重,选择一个权重较大的条目,替换另一个)来选择一个条目
#5
0
<?php
$inarray = range(0,100);
shuffle($inarray);
$outarray = array_slice($inarray, 0, 20);
?>
#6
0
Bonus question answer: Take a look at Roulette Wheel selection. The website talks about geentic algorithms, but the selection methods are sound and can be applied to a range of ideas.
附加问题回答:看看轮盘赌轮的选择。这个网站谈论的是方位算法,但是选择方法是合理的,可以应用于各种各样的想法。
#7
0
I have some code that sort of does what you ask for. I store a list of sponsorship links in a text file, and pick them at random. But, if I want to skew the set, I use multiple the links ;-)
我有一些代码可以做你想要的。我将赞助链接列表存储在一个文本文件中,并随机选择它们。但是,如果我想倾斜的集合,我使用多重的链接;
Sponsors file:
支持文件:
<a href="http://www.example.com">Example</a>
<a href="http://www.example.com">Example</a>
<a href="http://www.bbc.co.uk">BBC</a>
<a href="http://www.google.com">Google</a>
PHP:
PHP:
$sponsor_config = 'sponsors.txt';
$trimmed = file($sponsor_config, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$op = array();
$limit = 20; // insurance
$loops = 0;
$needed = 4;
$op[] = '<div id="sponsorship"><!-- start sponsorship -->';
$selected = array();
while ( (count($selected) < $needed) AND ($loops <= $limit)) {
$choice = rand(0, count($sponsors)-1);
if(!in_array($sponsors[$choice], $selected)) $selected[] = $sponsors[$choice];
$loops++;
}
foreach($selected as $k => $selection) {
$op[] = '<p class="sponsorship bg_'.($k%3+1).'">Click the link below to<br />visit our Site Sponsor:<br />'.$selection.'</p>';
}
$op[] = '</div><!-- end sponsorship -->';
return join("\n",$op)."\n";
V. quick and v.v. dirty... but it works
快速和V. V.肮脏…但它的工作原理
#1
16
How to select one or more random items from an array in php: http://us3.php.net/manual/en/function.array-rand.php
如何从php数组中选择一个或多个随机项:http://us3.php.net/manual/en/function.array-rand.php
How to do random weighted elements:
http://20bits.com/articles/random-weighted-elements-in-php/
如何做随机加权元素:http://20bits.com/articles/random-weighted- elements-inphp/
#2
21
You could shuffle
the array and then pick the first ten elements with array_slice
:
你可以洗牌数组,然后用array_slice选择前10个元素:
shuffle($array);
$tenRandomElements = array_slice($array, 0, 10);
#3
3
Example #1 array_rand() example
例# 1()用于示例
<?php
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
?>
#4
0
An array of arrays in PHP should be a good strategy. You can keep the data for these array in any way you like (hard-coded, XML, etc) and arrange it in the arrays as such:
PHP中的数组应该是一种很好的策略。您可以以任何您喜欢的方式(硬编码、XML等)保存这些数组的数据,并将其按如下方式排列:
Array {
Array (item0) { filename,description, weight,...}
Array (item1) { filename,description, weight,...}
Array (item2) { filename,description, weight,...}
}
You can then use the array_rand function to randomly remove items from the array. Creating a weight value for each entry will allow you to pick one entry over another using some sort of priority strategy (e.g. randomly get 2 items from the array, check weight, pick the one with a greater weight and replace the other)
然后可以使用array_rand函数从数组中随机删除项。为每个条目创建一个权重值将允许您使用某种优先策略(例如,从数组中随机获取两个条目,检查权重,选择一个权重较大的条目,替换另一个)来选择一个条目
#5
0
<?php
$inarray = range(0,100);
shuffle($inarray);
$outarray = array_slice($inarray, 0, 20);
?>
#6
0
Bonus question answer: Take a look at Roulette Wheel selection. The website talks about geentic algorithms, but the selection methods are sound and can be applied to a range of ideas.
附加问题回答:看看轮盘赌轮的选择。这个网站谈论的是方位算法,但是选择方法是合理的,可以应用于各种各样的想法。
#7
0
I have some code that sort of does what you ask for. I store a list of sponsorship links in a text file, and pick them at random. But, if I want to skew the set, I use multiple the links ;-)
我有一些代码可以做你想要的。我将赞助链接列表存储在一个文本文件中,并随机选择它们。但是,如果我想倾斜的集合,我使用多重的链接;
Sponsors file:
支持文件:
<a href="http://www.example.com">Example</a>
<a href="http://www.example.com">Example</a>
<a href="http://www.bbc.co.uk">BBC</a>
<a href="http://www.google.com">Google</a>
PHP:
PHP:
$sponsor_config = 'sponsors.txt';
$trimmed = file($sponsor_config, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$op = array();
$limit = 20; // insurance
$loops = 0;
$needed = 4;
$op[] = '<div id="sponsorship"><!-- start sponsorship -->';
$selected = array();
while ( (count($selected) < $needed) AND ($loops <= $limit)) {
$choice = rand(0, count($sponsors)-1);
if(!in_array($sponsors[$choice], $selected)) $selected[] = $sponsors[$choice];
$loops++;
}
foreach($selected as $k => $selection) {
$op[] = '<p class="sponsorship bg_'.($k%3+1).'">Click the link below to<br />visit our Site Sponsor:<br />'.$selection.'</p>';
}
$op[] = '</div><!-- end sponsorship -->';
return join("\n",$op)."\n";
V. quick and v.v. dirty... but it works
快速和V. V.肮脏…但它的工作原理