Random Numbers [SOLVED]

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

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Random Numbers [SOLVED]

Post by tecktalkcm0391 »

How can I get PHP to pick 5 random numbers, but where none of them are the same. I need numbers 1-75.
Last edited by tecktalkcm0391 on Thu Jul 20, 2006 2:01 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

this was seriously a rtfm
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Untested.

Code: Select all

function getRandomNumbers($total, $min, $max)
{
   $randoms = array();

   while (count($randoms) < $total)
   {
      $seed = rand($min, $max);
  
      if (!in_array($seed, $randoms))
      {
         $randoms[] = $seed;
      }
   }

   return $randoms;
}
Last edited by John Cartwright on Thu Jul 20, 2006 1:51 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Daedalus- wrote:this was seriously a rtfm
I believe this is more of a case of generating unique random numbers, not generating the numbers themselves.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

This should give you 5 different random numbers.

Untested..

Code: Select all

<?php

function randomNum($length)
{
    $Rand = range(1,75);
    $num = '';

    for ($i = 0; $i < $length; $i++) {

        $counter = 0;
        
        while (true)
        {
            $x = rand(0,74);

            if (isset($Rand[$x]))
            {
                $num .= $Rand[$x];
                unset($Rand[$x]);
                break;
            }

            $counter++;

            if ($counter > 100)
            {
                return 'Error!';
            }
        }
        
    }

    return $num;
}

$RandomNumbers = randomNum(5);

?>
ya beat me to it Jcart
Last edited by Benjamin on Thu Jul 20, 2006 1:46 pm, edited 2 times in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

$error never gets set (does it?).
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Everah wrote:$error never gets set (does it?).
Fixed..
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

WOW. THANKS EVERYONE! :D :D :D
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Jcart wrote:
Daedalus- wrote:this was seriously a rtfm
I believe this is more of a case of generating unique random numbers, not generating the numbers themselves.
ah, i see
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Holy cow, you lot always overdo stuff..

Code: Select all

$a=range(1,75); //Make an array of numbers
shuffle($a); //Shuffle them to make them random
print_r(array_slice($a,0,5)); //Grab the first five
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Good job onion2k :)
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

/agree
/clap
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Onion, you make me cry with joy :cry: .
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Everah wrote:Onion, you make me cry with joy :cry: .
Aww. Hugs.
Post Reply