Page 1 of 2
random numbers
Posted: Sun Apr 25, 2010 12:23 pm
by Richard_in_Az
I need to know how I can best work out loading an array with 104 unique numbers. That is, no two numbers in the list will ever be the same. In BASIC, that's fairly easy to do with being able to redirect programming as needed. But you can't do that in PHP.
Re: random numbers
Posted: Sun Apr 25, 2010 1:39 pm
by Benjamin
You actually
can do that, but it's a very bad idea. A good solution would be to populate an array with a range of numbers, then randomly select items from that array to populate 104 entries into another array.
Re: random numbers
Posted: Sun Apr 25, 2010 3:01 pm
by Eran
Just use mt_rand() in a loop
Re: random numbers
Posted: Sun Apr 25, 2010 3:18 pm
by Benjamin
Richard_in_Az wrote:104 unique numbers
Re: random numbers
Posted: Sun Apr 25, 2010 3:22 pm
by Eran
Was that in reply to me? you just have to check in each iteration that the number does not exist in the array. very simple. What does using an intermediate array help?
Re: random numbers
Posted: Sun Apr 25, 2010 3:52 pm
by Benjamin
pytrin wrote:Was that in reply to me? you just have to check in each iteration that the number does not exist in the array. very simple. What does using an intermediate array help?
Yes. That would work as well. I was thinking of using array_rand to pick out random entries, and since the array wouldn't contain any duplicates, no additional checks would be required.
Re: random numbers
Posted: Sun Apr 25, 2010 3:55 pm
by Eran
You would have to create a very large array for that to be effective, otherwise there would be a lot of overlap in the results (= not very random)
Re: random numbers
Posted: Sun Apr 25, 2010 4:00 pm
by Benjamin
Yeah I have no clue what the number range needs to be.
Re: random numbers
Posted: Mon Apr 26, 2010 10:29 am
by pickle
This might seem overly simplistic, but you didn't state what the numbers needed to be.
Re: random numbers
Posted: Mon Apr 26, 2010 12:42 pm
by Jonah Bron
Haha! That's awesome. Lets hear it for thinking outside of the box.
Here's another simple solution, if that's not suitable.
Code: Select all
$x = 0;
$array = array();
while ($x <= 103){
$y = mt_rand();
while (array_search($y, $array) !== false){
$y = mt_rand();
}
array_push($array, $y);
}
I just know somebody's going to come up with the equivalent of this in two lines...

Re: random numbers
Posted: Mon Apr 26, 2010 12:48 pm
by Eran
My idea was similar but simpler:
Code: Select all
$numbers = array();
while(count($numbers) < 104) {
if(! in_array($number = mt_rand(),$numbers)) {
$numbers[] = $number;
}
}
Re: random numbers
Posted: Mon Apr 26, 2010 1:08 pm
by Benjamin
Jonah Bron wrote:I just know somebody's going to come up with the equivalent of this in two lines...

I could do it on one line if shuffle() returned a shuffled array, rather than a bool.
Maybe I'll think of another way.
Re: random numbers
Posted: Mon Apr 26, 2010 1:19 pm
by Eran
One liner (PHP 5.3 version)-
Code: Select all
$numbers = array_map(function(){return mt_rand();},range(0,103));
Re: random numbers
Posted: Mon Apr 26, 2010 1:35 pm
by Benjamin
Nice!
Re: random numbers
Posted: Mon Apr 26, 2010 1:42 pm
by Eran
and a one-liner for older PHP versions (I had to get by the exact 2 parameter requirement on random functions)
Code: Select all
$numbers = array_map('mt_rand',range(0,103),range(1000000,1000103));