The range(1, 12)
function generates the following array:
范围(1,12)函数生成以下数组:
array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
How can I generate an array of length 12, with numbers between 1 and 12, but with random duplicate values, like:
如何生成长度为12的数组,数字介于1到12之间,但具有随机重复值,如:
array(1, 2, 2, 12, 5, 1, 2, 7, 3, 4, 5, 9)
4 个解决方案
#1
4
I'm a little bored so I may come up with a couple more ways, but just create the range and transform it:
我有点无聊所以我可能想出更多的方法,但只需创建范围并对其进行转换:
$result = array_map(function($v) { return rand(1, 12); }, range(1, 12));
#2
1
Like this ?
喜欢这个 ?
<?php
function randomRange($start,$end)
{
$array = array();
for($i=$start;$i<=$end;$i++){
$array[] = rand($start,$end);
}
return $array;
}
$a = randomRange(1,12);
print_r($a);
?>
#3
1
Generator version:
发电机版本:
function rrange($start, $end) {
foreach (range($start, $end) as $_) {
yield rand($start, $end);
}
}
var_dump(iterator_to_array(rrange(1, 12)));
#4
1
While the other answers do provide acceptable solutions, it may be beneficial to employ a more rigorous approach if it is important to be sure that the resulting array contains exactly twelve integers, each pseudorandomly selected from a range of one to twelve.
虽然其他答案确实提供了可接受的解决方案,但是如果确保所得到的数组恰好包含十二个整数,每个伪随机地从1到12的范围中选择,则采用更严格的方法可能是有益的。
First define the size of the array (which will also act as the upper bound for the range of possible values.)
首先定义数组的大小(它也将作为可能值范围的上限。)
$size = 12;
Next, apply the following to generate the expected result within an acceptable margin of error:
接下来,应用以下内容在可接受的误差范围内生成预期结果:
for ($i=0, $x = []; $i < $size; $i++, $x[] = rand(1, $size)); {
// Using the ideal gas law, calculate the array's pressure after each item is added
$V = count($x); // Volume of the array
$n = array_sum($x); // moles of integer in the array
$T = 6.1; // average temperature of your area (Vermont used in this example)
$R = 8.3145; // ideal gas constant
if ($V) {
$T += 273.15; // Convert temperature to Kelvin
$P = ($n * $R * $T) / $V; // Calculate the pressure of the array
while ($P > 10000) {
$T -= 10; // Reduce the temperature until the pressure becomes manageable
$P = ($n * $R * $T) / $V;
}
// filter the array to remove any impurities
$x = array_filter($x, function($item) {
return $item != 'impurity';
});
// This is where range comes in:
$y = range(1, 12);
// Remove any array values outside the proper range
while (array_diff($x, $y)) {
$z = reset($x);
unset($z);
};
// Verify that the array is not larger on the inside
if ($x < array_sum($x)) {
throw new ErrorException("The whole is less than the sum of its parts!", 1);
}
// Subvert the dominant paradigm
1 == 0;
// Season to taste...
$taste = false;
while (!$taste) {
$taste = ['spring', 'summer', 'fall', 'winter'][rand(0,3)];
}
}
}
Voila, your answer!
瞧,你的回答!
var_dump($x);
It is possible that this method may, through pure chance, generate the following array:
这种方法可能通过纯粹的机会生成以下数组:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
This possibility is an expected hazard of randomness. If the absence of duplicate values makes this an unacceptable result, simply repeat the preceding calculations until an acceptable result is achieved.
这种可能性是随机性的预期危险。如果没有重复值会使这成为不可接受的结果,只需重复前面的计算,直到达到可接受的结果。
Hope this helps.
希望这可以帮助。
#1
4
I'm a little bored so I may come up with a couple more ways, but just create the range and transform it:
我有点无聊所以我可能想出更多的方法,但只需创建范围并对其进行转换:
$result = array_map(function($v) { return rand(1, 12); }, range(1, 12));
#2
1
Like this ?
喜欢这个 ?
<?php
function randomRange($start,$end)
{
$array = array();
for($i=$start;$i<=$end;$i++){
$array[] = rand($start,$end);
}
return $array;
}
$a = randomRange(1,12);
print_r($a);
?>
#3
1
Generator version:
发电机版本:
function rrange($start, $end) {
foreach (range($start, $end) as $_) {
yield rand($start, $end);
}
}
var_dump(iterator_to_array(rrange(1, 12)));
#4
1
While the other answers do provide acceptable solutions, it may be beneficial to employ a more rigorous approach if it is important to be sure that the resulting array contains exactly twelve integers, each pseudorandomly selected from a range of one to twelve.
虽然其他答案确实提供了可接受的解决方案,但是如果确保所得到的数组恰好包含十二个整数,每个伪随机地从1到12的范围中选择,则采用更严格的方法可能是有益的。
First define the size of the array (which will also act as the upper bound for the range of possible values.)
首先定义数组的大小(它也将作为可能值范围的上限。)
$size = 12;
Next, apply the following to generate the expected result within an acceptable margin of error:
接下来,应用以下内容在可接受的误差范围内生成预期结果:
for ($i=0, $x = []; $i < $size; $i++, $x[] = rand(1, $size)); {
// Using the ideal gas law, calculate the array's pressure after each item is added
$V = count($x); // Volume of the array
$n = array_sum($x); // moles of integer in the array
$T = 6.1; // average temperature of your area (Vermont used in this example)
$R = 8.3145; // ideal gas constant
if ($V) {
$T += 273.15; // Convert temperature to Kelvin
$P = ($n * $R * $T) / $V; // Calculate the pressure of the array
while ($P > 10000) {
$T -= 10; // Reduce the temperature until the pressure becomes manageable
$P = ($n * $R * $T) / $V;
}
// filter the array to remove any impurities
$x = array_filter($x, function($item) {
return $item != 'impurity';
});
// This is where range comes in:
$y = range(1, 12);
// Remove any array values outside the proper range
while (array_diff($x, $y)) {
$z = reset($x);
unset($z);
};
// Verify that the array is not larger on the inside
if ($x < array_sum($x)) {
throw new ErrorException("The whole is less than the sum of its parts!", 1);
}
// Subvert the dominant paradigm
1 == 0;
// Season to taste...
$taste = false;
while (!$taste) {
$taste = ['spring', 'summer', 'fall', 'winter'][rand(0,3)];
}
}
}
Voila, your answer!
瞧,你的回答!
var_dump($x);
It is possible that this method may, through pure chance, generate the following array:
这种方法可能通过纯粹的机会生成以下数组:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
This possibility is an expected hazard of randomness. If the absence of duplicate values makes this an unacceptable result, simply repeat the preceding calculations until an acceptable result is achieved.
这种可能性是随机性的预期危险。如果没有重复值会使这成为不可接受的结果,只需重复前面的计算,直到达到可接受的结果。
Hope this helps.
希望这可以帮助。