Page 2 of 2

Re: random numbers

Posted: Mon Apr 26, 2010 1:49 pm
by Benjamin
Actually the first one results in a parse error. I'm not sure they guarantee unique numbers either.

This was the direction I was headed, but it of course does not work because shuffle doesn't return anything useful.

Code: Select all

$random = array_slice(shuffle(range(0, 25000)), 0, 103);
This should work ok:

Code: Select all

$pool = range(0,25000);
shuffle($pool);
$random = array_slice($pool, 0, 103);
Maybe using a combination of array_map and array_rand would result in a good one liner.

Re: random numbers

Posted: Mon Apr 26, 2010 1:50 pm
by Eran
Actually the first one results in a parse error. I'm not sure they guarantee unique numbers either.
Works for me on PHP 5.3.1. Why would mt_rand() not guarantee random numbers?

Re: random numbers

Posted: Mon Apr 26, 2010 1:52 pm
by Benjamin
Ah ok, I'm running 5.2.10.

Unique, not random.

Re: random numbers

Posted: Mon Apr 26, 2010 1:56 pm
by Eran
One liner (PHP 5.3 version)-
lambdas were added in 5.3
Unique, not random.
Ah, I see what you mean. Nice try though

Re: random numbers

Posted: Mon Apr 26, 2010 2:14 pm
by Jonah Bron
Foul! Multiple semicolons on one line!

@pytrin, Where's the shooting-self-in-head-with-gun emoticon?

Re: random numbers

Posted: Mon Apr 26, 2010 2:49 pm
by Benjamin
Figured it was possible. This isn't too inefficient either.

Code: Select all

$numbers = array_slice(array_unique(array_map(create_function('', 'return mt_rand(0,250000);'), range(0,150))), 0, 104);