b3ta.com board
You are not logged in. Login or Signup
Home » Messageboard » XXX » Message 9398075 (Thread)

# hahaha excellent
tj: anyone good at PHP? I need a function to generate 5 (say) random unique numbers
(, Mon 27 Apr 2009, 12:07, archived)
# 25
163
988
10333
1236500983

Done
(, Mon 27 Apr 2009, 12:09, archived)
# cor
ta

*breaks website*
(, Mon 27 Apr 2009, 12:09, archived)
# that's Numberwang!
(, Mon 27 Apr 2009, 13:00, archived)
# Feel free to change rand() to the random function of your choice.
$numbers = array();
while (count($numbers) < 5) $numbers[rand()] = count($numbers);
$numbers = array_flip($numbers);
(, Mon 27 Apr 2009, 13:28, archived)
# clevar
(as long as range of my_rand >> 5, and don't even think of using it to generate a big permutation :-)

EDIT: Haaang onn... I don't think that works? What if rand() does return the same result twice: you'll get two entries in $numbers with the same value (of count), and then array_flip will choose the later one, and count($numbers) will now be 4. Surely?

I think you want:

$numbers = array();
while (count($numbers) < 5) $numbers[rand()] = 1;
$numbers = array_keys($numbers);

(But I've never written a line of PHP in my life before, so it's your risk :-) *gazzes Mighty Nibus & benryves*
(, Mon 27 Apr 2009, 14:16, archived)
# Sorry,
you're correct. If using the count() method to set the array value using a random key you'd need to remove the old value first in case of duplicates, which sort of defeats the point of the exercise.
I forgot about array_keys(), your method should work.
(, Mon 27 Apr 2009, 19:08, archived)
# 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:
<?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"]
(, Tue 28 Apr 2009, 11:31, archived)
# 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)