Oh no, another bug:
Nibus, do you wish to treat the chosen values as an ordered sequence, or as a set? If you treat them a sequence, the code snippet is probably still wrong:
The definition of array_keys (http://uk.php.net/function.array-keys) does not specify the order in which it lists the keys.
It is perfectly legal, and (imho) quite likely, for the implementation to always return the keys in some particular order (for example: in numerical order of their hash-values). This would mean that if the five chosen numbers were (say) 32, 55, 12, 10, 65, then they would ONLY EVER be returned in that order. You would simply never ever see 55, 12, 10, 65, 32. In other words: not very random! They might even be sorted numerically. They might be sorted on some platforms, and not on others. It might change when you upgrade to a new version of PHP. Awoooga! Awooga! Don't go there.
I'll have a go at a new code snippet...
...Here:
Fun this, isn't it!
*gazzes MN and br again*
'The late Roger Needham once remarked that ‘optimisation is the process of
taking something that works and replacing it with something that almost works,
but is cheaper’.' ['Optimised to fail: Card readers for online banking']
( ,
Tue 28 Apr 2009, 11:31,
archived)
The definition of array_keys (http://uk.php.net/function.array-keys) does not specify the order in which it lists the keys.
It is perfectly legal, and (imho) quite likely, for the implementation to always return the keys in some particular order (for example: in numerical order of their hash-values). This would mean that if the five chosen numbers were (say) 32, 55, 12, 10, 65, then they would ONLY EVER be returned in that order. You would simply never ever see 55, 12, 10, 65, 32. In other words: not very random! They might even be sorted numerically. They might be sorted on some platforms, and not on others. It might change when you upgrade to a new version of PHP. Awoooga! Awooga! Don't go there.
I'll have a go at a new code snippet...
...Here:
<?php
$have = array();
$sequence = array();
while (count($sequence) < 5) {
// pick a number
r = rand() % 1000; // (1000, or whatever upper limit)
if (! array_key_exists(r, $have)) {
// it's new, so remember we have it, and add to sequence
$have[r] = 1;
$sequence[count($sequence)] = r;
}
}
// your sequence is now stored in $sequence[0] to $sequence[4]
?>
Fun this, isn't it!
*gazzes MN and br again*
'The late Roger Needham once remarked that ‘optimisation is the process of
taking something that works and replacing it with something that almost works,
but is cheaper’.' ['Optimised to fail: Card readers for online banking']
That makes sense (though you'd need $r instead of r)!
That said, rand()%$max is generally unwise as it skews the distribution. (rand()/getrandmax())*$max won't skew the distribution. PHP also provides a rand($min, $max) overload - I'm not sure which method that uses, but it's probably easier to use that!
( ,
Tue 28 Apr 2009, 14:13,
archived)
Gaaaahhhh!!! The more I look, the worse it gets!
PHP:rand() says: "On some platforms (such as Windows), getrandmax() is only 32768."
WHAT??!!! That's like helpfully including free exploding poisoned razor blades with the programming language you ordered!
"... consider using mt_rand() instead."
It depends what you want the pseudo-randomness for, but if patterning will have a serious bad effect, you might want to use mt_rand() instead. Don't forget to think about how it is seeded, too!
( ,
Tue 28 Apr 2009, 14:33,
archived)
WHAT??!!! That's like helpfully including free exploding poisoned razor blades with the programming language you ordered!
"... consider using mt_rand() instead."
It depends what you want the pseudo-randomness for, but if patterning will have a serious bad effect, you might want to use mt_rand() instead. Don't forget to think about how it is seeded, too!