Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I try to output randomly some numbers from an array of possible values.

The problem that I have is with the recursion, so that my function checking if the number generated randomly was already used before starts again if the number was already used (and is not in the array of possible values anymore).

I get an infinite loop which I also don't understand.

Here is my code:

<!DOCTYPE html>
<html>
<head>
<title></title>
<script>
    // Global array of possible numbers
    var possibleNumbers = [1, 2, 3, 4];

    // Check if element in the array
    function inArray(needle, haystack) {
        var length = haystack.length;
        for(var i = 0; i < length; i++) {
            if(haystack[i] == needle) return true;
        }
        return false;
    }

    // Random number between 1 and 4
    function genRand(){return Math.floor(1+ (4 * Math.random()));}

    // Return an random number from the possible numbers in the global array
    function randNumFromArray() {
        var generatedNum = genRand();
        console.log('possibleNumbers so far:' + possibleNumbers);
        console.log('generatedNum: ' + generatedNum);

        // Restart as long as the number is not in the array (not already used)
        while(inArray(generatedNum,possibleNumbers) === false) {
            console.log('Generating again...');
            randNumFromArray();
        }

        console.log('generatedNum not used yet, using it');

        // Use that number and remove it from the array
        var index = possibleNumbers.indexOf(generatedNum);
        if (index > -1) {
            possibleNumbers.splice(index, 1);
        }

        console.log('Removing the number from the array');
        console.log('possibleNumbers after removal:' + possibleNumbers);
        return generatedNum;
    }

    // Calling the function 4 times to get the 4 possible numbers in a random order 
    randNumFromArray();
    randNumFromArray();
    randNumFromArray();
    randNumFromArray();
</script>
</head>
<body>
</body>
</html>
share|improve this question
add comment

1 Answer

Lets debug your code,

  1. Math.random() // Returns a random number between 0 (inclusive) and 1 (exclusive)

  2. 4 * Math.random() // will be number either be 0 or 4 < number < 5.

  3. 1+ (4 * Math.random()) // In your Case you are probably waiting for Math.random() to generate 0 which of least probability (because any number other than zero will make the total sum to 5.xx)

  4. Math.floor(1+ (4 * Math.random()) // will surely not be in your list [1, 2, 3, 4]; if number generate > 0 then will cross 4 so It will loop continuously

If you want to generate random number of specific range then use,

/**
 * Returns a random number between min and max
 */
function getRandomArbitary (min, max) {
    return Math.random() * (max - min) + min;
}

Refer for generating random number of specific range

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.