1

I am running a function, and part of it requires to pick a random number. However, it cannot be in an array of already selected numbers. So if the random number chosen is in the array, it needs to select a new number. I seem to be really struggling with something that seems so easy.

function choice() {
    global $countries;
    global $count;
    global $answers;
    global $choice;

    $i = rand(0, $count - 1);
    if(in_array($i, $answers))
        choice();
    else {
        $answers[] = $i;
        $choice = $countries[$i]['capital_city'];
        return $choice;
    }

So the current logic here, is it selects a random number, checks if it’s in the array. If it isn’t then it sets the variable and returns it. If it is, then it generates a new number by restarting the function. When it does find one in the function, it returns an empty result, rather than going back through the function. What am I doing so wrong?

5
  • 1
    This has a syntax error. Commented Jun 28, 2016 at 23:22
  • 2
    We can't someone who doesn't help us first by telling us what the precise error and location is Commented Jun 28, 2016 at 23:23
  • 1
    You don't return the recursive call. Commented Jun 28, 2016 at 23:24
  • have a look at this question, seems very similar: stackoverflow.com/a/17110791/1823684 Commented Jun 28, 2016 at 23:25
  • 3
    You know functions can take arguments, right? Use of globals is generally a bad thing and should only be used when that is your last possible pragmatic choice. Commented Jun 28, 2016 at 23:47

2 Answers 2

2

Add a return statement before the recursive call.

function choice() {
    global $countries;
    global $count;
    global $answers;
    global $choice;

    $i = rand(0,$count-1);

    if(in_array($i,$answers)) return choice();
    else {
        $answers[] = $i;
        $choice = $countries[$i]['capital_city'];
        return $choice;
    }
Sign up to request clarification or add additional context in comments.

5 Comments

That seems to work, I knew i was missing something simple like that, but I couldnt work it out.
However, what is better, this way, or using a while loop?
I believe the while loop will have less overhead. Would also use references in place of global
@Source the "correct" answer is meaningless if this is the code you end up with.
@naomik No regrets, #globals for life.
2

If you don’t need to use recursion, you could just use a while loop:

function choice() {
    global $countries;
    global $count;
    global $answers;
    global $choice;

    $i = rand(0, $count - 1);

    while (in_array($i, $answers)) {
        $i = rand(0, $count - 1);
    }

    $answers[] = $i;
    $choice = $countries[$i]['capital_city'];
    return $choice;
}

Or use this do while in place of the while;

do {
    $i = rand(0, $count-1);
} while (in_array($i, $answers));

2 Comments

count value didnt need to be altered.
I think a do while loop would maybe look a bit better here :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.