Jump to content

rand() filter??


mrbean

Recommended Posts

Hi there,

 

I have one question about the function rand(),

 

What if i want to have an number between 0 and 360

 

Without 90 to 180

 

so 91,92,93,94.....180 can't be the number

 

how can i do that?

 

btw, I know its possible through if,while,for and so on. But is there an other way to do this?

Link to comment
Share on other sites

There's no magic solution.  CV has a cool approach.  You could also write a quick function like this.  Rather than asking for explanations of how these work, try reading about the functions used in the php manual.  They are well documented.

 

function getRand() {
  $x = rand(0, 270);
  if ($x > 89 && $x     $x += 180;
  return $x;
}

Link to comment
Share on other sites

$range = array_merge(range(0,89),range(181,360));
$number = $range[array_rand($range)];
echo $number;

 

what crayon did there is he created 2 arrays with the min and max so that they excluded the numbers that you want to exclude using the range() function, then he merged those two arrays using the array_merge() function. then using the array_rand() function, this chooses a random number from the array that he merged. then he echoes the chosen number. Not sure if this is what you wanted from an explanation.. ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.