|
|
|
| Need a little help. | |
Posted: 03-20-2003 05:55 AM |
|
|
M0nKeY |
- Remember - |
|
|
Joined: 09 Feb 2002 |
Posts: 1235 |
|
|
|
|
|
|
|
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 |
|
_________________ "Am I a man dreaming of a butterfly or a butterfly dreaming of a man?" |
|
|
|
Posted: 03-20-2003 10:57 AM |
|
|
Shn |
R.I.P. |
|
|
Joined: 10 Feb 2002 |
Posts: 738 |
|
|
|
|
|
|
|
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) |
|
|
|
|
Posted: 03-20-2003 02:10 PM |
|
|
M0nKeY |
- Remember - |
|
|
Joined: 09 Feb 2002 |
Posts: 1235 |
|
|
|
|
|
|
|
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. |
|
|
|
|
| | |
Posted: 03-20-2003 05:16 PM |
|
|
Shn |
R.I.P. |
|
|
Joined: 10 Feb 2002 |
Posts: 738 |
|
|
|
|
|
|
|
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. |
|
|
|
|
| | |
Posted: 03-20-2003 05:18 PM |
|
|
M0nKeY |
- Remember - |
|
|
Joined: 09 Feb 2002 |
Posts: 1235 |
|
|
|
|
|
|
|
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. |
|
_________________ "Am I a man dreaming of a butterfly or a butterfly dreaming of a man?" |
|
|
|
Mindcontroll Forum Index » Programs/Programming Development |
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You can vote in polls in this forum
|
All times are GMT - 5 Hours
Page 1 of 1
|
|
|
|
|
Video Games Suck - XXXSwim - Archive |
Powered by phpBB © 2001-2005 phpBB Group. |
Designed for Trushkin.net | Themes Database |
|