random numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Richard_in_Az
Forum Newbie
Posts: 12
Joined: Wed Apr 14, 2010 2:12 am

random numbers

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: random numbers

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: random numbers

Post by Eran »

Just use mt_rand() in a loop
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: random numbers

Post by Benjamin »

Richard_in_Az wrote:104 unique numbers
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: random numbers

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: random numbers

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: random numbers

Post 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)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: random numbers

Post by Benjamin »

Yeah I have no clue what the number range needs to be.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: random numbers

Post by pickle »

This might seem overly simplistic, but you didn't state what the numbers needed to be.

Code: Select all

$numbers = range(0,103);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: random numbers

Post by Jonah Bron »

Haha! That's awesome. Lets hear it for thinking outside of the box. 8)

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... :|
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: random numbers

Post 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;
    }
}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: random numbers

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: random numbers

Post by Eran »

One liner (PHP 5.3 version)-

Code: Select all

$numbers = array_map(function(){return mt_rand();},range(0,103));
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: random numbers

Post by Benjamin »

Nice!
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: random numbers

Post 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));
Post Reply