0

The range(1, 12) function generates the following array:

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:

array(1, 2, 2, 12, 5, 1, 2, 7, 3, 4, 5, 9)
1
  • Do a simple loop and create a random number between 1 and 12 and add it to the array until you have the amount of elements you want. Commented Feb 3, 2017 at 20:55

4 Answers 4

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));
Sign up to request clarification or add additional context in comments.

Comments

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

?>

2 Comments

Nice, but save a line with $array[] = rand($start,$end);
Thanks for the tip!
1

Generator version:

function rrange($start, $end) {
    foreach (range($start, $end) as $_) {
        yield rand($start, $end);
    }
}

var_dump(iterator_to_array(rrange(1, 12)));

Comments

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.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.