Hi. As title said : I have an array of 30 values and i need to extract from this array 3 different random values. How can do it? cheers
Any particular reason you're calling `shuffle`?
Tim Cooper
2010-10-15 20:16:15
just to make sure it is realllllly random :) .. the history behind it is the fact that last year using array_rand i was seeing not-so-random results (similar items on consecutive runs), so to make it a bit more random i would shuffle the array first and then feed to the array_rand function and it seemed to give more random values.
Sabeen Malik
2010-10-15 20:18:05
+5
A:
Shamelessly stolen from the PHP manual:
<?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";
?>
Note that, as of PHP 5.2.10, you may want to shuffle (randomize) the keys that are returned via shuffle($rand_keys)
, otherwise they will always be in order (smallest index first). That is, in the above example, you could get "Neo, Trinity" but never "Trinity, Neo."
If the order of the random elements is not important, then the above code is sufficient.
konforce
2010-10-15 20:14:20