3

I want to write a function that checks if a random number is equal to a previous random number and returns a new random number not equal to the previous one. I want to use recursion to do this but I'm not sure if this is the correct syntax.

function newNumber(next,previous) {
    if (next != previous)
        return next;
    else {
        next = Math.floor(Math.random()*10);
        newNumber(next, previous);
    }
}

What would be the best way to get this to work?

3
  • No, recursion is not the right method here. Don't overuse recursion when it's not needed. Commented Jun 2, 2013 at 6:13
  • 1
    Do you want to compare the number to all previous used random numbers, or just the last returned random number--ie you never want this new number to return the same number two times in a row. Commented Jun 2, 2013 at 6:14
  • the latter. I want the function to always show a new value in comparison to the previous one. Commented Jun 2, 2013 at 6:17

4 Answers 4

7

I would ditch the recursion for this altogether. Just store the last random number as a property of the function itself, and the next time the user wants a random number, just return the first one you compute that's different from the last one.

Something like -

function newNumber() {
    var nextValue;
    while ((nextValue = Math.floor(Math.random()*10)) === newNumber.previous) ;

    newNumber.previous = nextValue;
    return nextValue;
}
Sign up to request clarification or add additional context in comments.

8 Comments

Side question: is it preferred to use a property, or to use a closure?
@Alan - a closure is just a fancy way of saying that a function "remembers" the context ("activation context") which existed when it was created. It's not really an either or type of thing with recursion, and doesn't really apply here, as far as I can tell.
@Alan: Both cases are similarly valid. I would use "closure", though, as it does not make the previous available outside the function scope. Plus, it would be easier to build several functions calculating random number independently (so last value from some unrelated call would not be treated as your call's last result).
@Alan, I don't think there is a difference, but it's easier to me reading closure. (just finished writing the answer and saw this debate...)
@AdamRackis, ... in theory. in practice it doesn't matter, no one will change it unless you want. this private issue is more paranoia than a real threat... :)
|
3

Closure way:

var newNumber = (function () {
    var previous;

    return function () {
        var nextValue;
        while ((nextValue = Math.floor(Math.random() * 10)) === previous);

        previous = nextValue;
        return nextValue;
    };
})();

Fiddle

1 Comment

@AdamRackis, I kinda copied your code and adapting it to closure, so it might seems familiar... :)
3

You don't need recursion for that. Actually you don't even need a loop. Just pick a random number from the numbers that are not the previous number:

function newNumber(previous) {
  var next = Math.floor(Math.random()*9);
  if (next >= previous) next++;
  return next;
}

6 Comments

Now it isn't random. The probability of getting previous+1 is more :D
This is just wrong random implementation, you could use a for loop and skip the Math.random... and I believe he doesn't want to store the previous value.
As I and Arjun wrote before , 1. the probably to get the previous number +1 is bigger than the rest. 2. you have to store the previous number to make it work, it doesn't seem to be what he wanted. Do you really think it's a random number generator?
@gdoron: 1. You are mistaken. There is not a bigger chance to get previous+1. Read the code! 2. The op is sending the previous number into the function in the original code, why do you think thats not what he wants to do?
1. A demo showing the problem 2. You're right, I was confused by Adam's answer, I watched it, and even copied the main function so I forgot what was the starting point, I apologize!
|
1

Just add return to newNumber(next, previous); in else block. The code goes like this now:

function newNumber(next,previous) {
    if (next != previous)
        return next;
    else {
        next = Math.floor(Math.random()*10);
        return newNumber(next, previous);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.