mrbean Posted May 3, 2011 Share Posted May 3, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/235454-rand-filter/ Share on other sites More sharing options...
.josh Posted May 3, 2011 Share Posted May 3, 2011 I guess one way to do it: $range = array_merge(range(0,89),range(181,360)); $number = $range[array_rand($range)]; echo $number; Quote Link to comment https://forums.phpfreaks.com/topic/235454-rand-filter/#findComment-1210106 Share on other sites More sharing options...
mrbean Posted May 3, 2011 Author Share Posted May 3, 2011 It works but can u explain what it does (detailed )? So i can do it myself. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/235454-rand-filter/#findComment-1210108 Share on other sites More sharing options...
gizmola Posted May 3, 2011 Share Posted May 3, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/235454-rand-filter/#findComment-1210110 Share on other sites More sharing options...
fugix Posted May 3, 2011 Share Posted May 3, 2011 $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.. Quote Link to comment https://forums.phpfreaks.com/topic/235454-rand-filter/#findComment-1210112 Share on other sites More sharing options...
mrbean Posted May 3, 2011 Author Share Posted May 3, 2011 thanks for explaining fugix and thanks for the advice gizmola. What i am gonna do is make an function of it and use it multiple times for my captcha script Quote Link to comment https://forums.phpfreaks.com/topic/235454-rand-filter/#findComment-1210119 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.