Does anyone know what's the randomness of PHP's shuffle()
function? Does it depend on the operating system?
Does it use PHP's own seeder?
Is it possible to use mt_rand()
as generator?
Does anyone know what's the randomness of PHP's shuffle()
function? Does it depend on the operating system?
Does it use PHP's own seeder?
Is it possible to use mt_rand()
as generator?
Its better to use an array to achieve this.
$options = array(
'Option One',
'Option Two',
'Option Three',
'Option Four'
);
//Shuffle the array
shuffle($options);
You can access the options like this (The order will be random);
$options[0];
$options[1];
$options[2];
$options[3];
Shuffle it with shuffle()
:
shuffle($xbb);
Searching Google for php shuffle array will give you tons of results as well, by the way.
Given numbers between 1 and 100.
Given numbers between 1 and 1000.
and so on.
So if you select some at random, then that vast majority of selected numbers will have the same number of digits, because the vast majority of possible values have the same number of digits.
Math.random()
is based on java.util.Random
, which is based on a linear congruential generator. That means its randomness is not perfect, but good enough for most tasks, and it sounds like it should be sufficient for your task.
However, it sounds like you're using the double
return value of Math.random()
to choose between a fixed number of choices, which may further degrade the quality of the randomness. It would be better to use java.util.Random.nextInt()
- just be sure to reuse the same Random
object.
Sometimes, it doesn't appear so random by looking at a snapshot on a resource pool to see which pieces it's getting at that instant
Our brains are really good at spotting patterns in perfect randomness, so that means almost nothing.
shuffle()
function is based on the same generator asrand()
, which is the system generator based on linear congruential algorithm. This is a fast generator, but with more or less randomness. Since PHP 4.2.0, the random generator is seeded automatically, but you can usesrand()
function to seed it if you want.mtrand()
is based on Mersenne Twister algorithm, which is one of the best pseudo-random algorithms available. To shuffle an array using that generator, you'd need to write you own shuffle function. You can look for example at Fisher-Yates algorithm. Writing you own shuffle function will yield to better randomness, but will be slower than the builtin shuffle function.