Does any one know which of the two choices for random number generation is going to produce a better randomness:
<?php
$array = array(1,2,3,4,5,6);
shuffle($array);
echo $array[0];
//// OR ////
echo rand(1,6);
?>
Or perhaps theres an even better option I am unaware of?
Both use the same PRNG so their randomness is equally good/bad. Obviously a plain
rand(1,6)is faster since it’s just the math and not both the math and the array stuff. Besides that, you’d usearray_rand()if you wanted a random element from an array.PHP also has the mersenne twister (
mt_rand()) which is “better” but unsuitable for cryptography (probably not relevant in your case).If you need true randomness you could consider reading from
/dev/random– but that read may block if there’s no randomness available. You could also use a hardware device that gives you even better randomness based on some physical effect.