Why is it this sometimes returns 2?
function pickServer(){
$varr = rand(1,4);
if($varr==2){
pickServer();
}
return $varr;
}
Why is it this sometimes returns 2?
function pickServer(){
$varr = rand(1,4);
if($varr==2){
pickServer();
}
return $varr;
}
Because you're not stopping the function there. the fourth line should read:
return pickServer();
Because when the value is 2 you don't return pickserver. And the function continues to return $varr.
It calls the function recursively to get another number that isn't 2, but does nothing with that result.
Try changing pickServer() to return pickServer().
Better still, write the function iteratively so that it just loops until the value returned isn't 2.
function pickServer(){
$varr = rand(1,4);
if($varr==2){
return pickServer(); //leave here
}
return $varr;
}
What you probably want is
function pickServer(){
$varr = rand(1,4);
if($varr==2){
$varr = pickServer();
}
return $varr;
}
-- but note that there's no guarantee that this doesn't go into a too long recursion. Maybe you should rather do something like this:
function pickServer(){
$varr = rand(1,3);
if($varr > 1){
$varr = $varr + 1;
}
return $varr;
}
You forgot to return the value...
function pickServer(){
$varr = rand(1,4);
if($varr==2){
return pickServer();
}
return $varr;
}
Another way to do it is using do … while
:
function pickServer() {
do {
$varr = rand(1,4);
} while ($varr == 2);
return $varr;
}
The answer to your question, as others have pointed out, is that your code falls through without returning. If 2
is returned by the call to rand() on both the first attempt and the second attempt (there's a 1/16 chance of this happening), you'll get 2
as a result.
But your approach to solving the problem could be better.
Both recursion and looping are barmy for this problem. This is a mapping problem, not a randomness problem. (It resembles some common randomness coding interview problems which can be handled most easily in a rejection loop, but it really isn't a problem of that class.)
You want one of three outcomes, not four. (1, 3, and 4.) That means you should be generating a range of three random numbers, not four. You could remap with an array or use an if
. Both possibilities are shown below. Let me know if I have syntax wrong--my PHPfu is weak this morning.
/* array remapping */
function pickServer() {
$remap = array(1, 3, 4);
return $remap[rand(1,3)];
}
/* if remapping */
function pickServer() {
$server = rand(1,3);
if ($server==2) {
$server=4;
}
return $server;
}
I didn't notice it before, but balpha anticipated my answer. He remapped with an if
in his second example. Instead of remapping 2 to 4, he just added one to any answer above 1, which is an equivalent solution.
I would so something like this:
function pickServer()
{
$servers = array(1,3,4);
return $servers[rand(1,count($servers))];
}
You can remove the recursion and remap a randomly selected 2. Simply decrease the range and map the beginning of the range (in this case 2) to 1.
function pickServer(){
$varr = rand(2,4);
if($varr==2){
return 1;
}
return $varr;
}