Mindcontroll Forum Index Mindcontroll

 

Need a little help.
Click here to go to the original topic

 
       Mindcontroll Forum Index -> Programs/Programming Development
View previous topic :: View next topic  
Author Message
M0nKeY



Joined: 09 Feb 2002
Posts: 1235

Posted: 03-20-2003 05:55 AM    Post subject: Need a little help.  

Hey,

I need a small PHP script that will echo 6 random numbers between 1-10 but once a number has been selected it can not be used again. Is ther any simple way to do this?

ty
Back to top  
Shn



Joined: 10 Feb 2002
Posts: 738

Posted: 03-20-2003 10:57 AM    Post subject:  

Code: function random_number()
{
   $valid_numbers = "0123456789";
   $valid_numbers_length = strlen($valid_numbers);
   for ($i=0; $i<6; $i++)
   {
      $burp=$valid_numbers[rand(0,--$valid_numbers_length)];
      $valid_numbers=str_replace($burp, "", $valid_numbers);
      echo ++$burp;
      echo '<br />';
   }
}

random_number();

There are MANY other ways to do this. I did it in 5 minutes...

I didn't add comments cause i believe comments deserve money ! (ah wait, maybe i'm just too lazy to explain)
Back to top  
M0nKeY



Joined: 09 Feb 2002
Posts: 1235

Posted: 03-20-2003 02:10 PM    Post subject:  

I was using a loop to compare 6 seperate variables, it was messy and crappy. This is way better. Is there a way that it can select more than 10 numbers though? LIke 01,02,13,07,21,05

Thanks.
Back to top  
Shn



Joined: 10 Feb 2002
Posts: 738

Posted: 03-20-2003 05:16 PM    Post subject:  

Code:
function random_number2($min, $max, $items)
{
   //$min: minimum number...
   //$max: maximum number...
   //$items: how many DISTINCT numbers you want to be randomly found

   /*
   eng: this function searches <$items> randomly generated DISTINCT numbers that go from <$min> to <$max>
   */

   //checking the vars are valid
   if($min!=$max)
   {
      if($min>$max)
      {
         $min+=$max;
         $max=$min-$max;
         $min=$min-$max;
      }

      //example: $min=1; $max=5; --> numbers : 5.
      // but 5-1==4, so gotta add 1 to it.
      $numbers_total=$max-$min+1;

      if($items<=$numbers_total)
      {
         //filling up the array with numbers
         for($i=0; $i<$numbers_total; $i++)
         {
            $numbers[$i]=$i+$min;
         }

         //getting the random numbers
         for($i=0; $i<$items; $i++)
         {
            $burgle=rand(0, --$numbers_total);
            $french_cuddly_var[$i]=$numbers[$burgle];
            //echo $numbers[$burgle].'<br />';
            $numbers[$burgle]=$numbers[$numbers_total];
            $numbers[$numbers_total]='';
         }
      }
      else   //error
         $french_cuddly_var='ERROR ERROR OMG ERROR !';
   }
   else   //error
      $french_cuddly_var='ERROR ERROR OMG ERROR !';

   return $french_cuddly_var;
}


Happy ??
I bet you can highly optimize this, but i've got no idea how.


It'll be 200 euros.
I accept checks, credit cards, cash.
Back to top  
M0nKeY



Joined: 09 Feb 2002
Posts: 1235

Posted: 03-20-2003 05:18 PM    Post subject:  

Shn wrote:
It'll be 100 euros.
I accept checks, credit cards, cash. Kickass... but if you can't accept PayPal or Epassporte I cant help you.
Back to top  
 
       Mindcontroll Forum Index -> Programs/Programming Development
Page 1 of 1


Video Games Suck - XXXSwim