0

I have an annoying script I can't complete.

I need 32 non-repeating numbers out of a possible 0-64 number set. Every time I try to create a loop to check the new random number against all the numbers in the array I end up with nothing, or an infinite loop.

I'm stumped :(

3

2 Answers 2

3

Try this:

var keys = [], numbers = [], x, total = 0;
while(total < 32) {
    x = Math.floor(Math.random() * 64);
    // way faster that looping through the array to check if it exists
    if(keys[x] == undefined) { 
        keys[x] = 1;
        numbers.push(x);
        total++;
    }
}
console.log(numbers);

Working Demo

Sign up to request clarification or add additional context in comments.

10 Comments

This will never return 64 but it will return 0. Math random is 0 inclussive to 1 exclusive - developer.mozilla.org/en-US/docs/JavaScript/Reference/… - that page also has good suggestions on how to implement random int between x and y
Thanks, that's close, but all the numbers in the array are sequential. Is there a quick way to randomize them?
@PeterAjtai - The OP needed the numbers between 0 and 64 specifically. Hence the floor call.
@flyagaricus - For that, you can directly use stackoverflow.com/questions/2450954/…
@PeterAjtai - You're right. This will never return 64. Answer corrected.
|
0

This code will generate a non repeating random number between 0 and whatever number you give it and will start over when it has been called the amount of the number you give it.

Give it a try:

let randomNumber = function (max) {
    let min = 0, prevIndexes = [];
    function exec(max2) {
        max = max || max2;
        let result = Math.floor(Math.random() * (max - min + 1) + min);
        if (prevIndexes) {
            if (prevIndexes.length - 1 === max) {
                clear();
            }
            let foundDouble, eqPrevInn = true;
            while (eqPrevInn) {
                foundDouble = false;
                result = Math.floor(Math.random() * (max - min + 1) + min);
                for (let i = 0, l = prevIndexes.length; i < l; i++) {
                    if (result === prevIndexes[i]) {
                        foundDouble = true;
                        break;
                    }
                }
                if (!foundDouble) {
                    eqPrevInn = false;
                }
            }
        }
        prevIndexes.push(result);
        console.log(prevIndexes);
        return result;

    }

    let clear = function () {
        if (prevIndexes) {
            prevIndexes = [];
            //             console.log('prevIndexes has been cleared');
        }
        else {
            //             console.log('already clear');
        }
    }
    return {
        exec: exec,
        clear: clear
    }
};

let random32 = randomNumber(32/*range*/).exec;

for (let i = 0; i <= 32; i++) {
    random32();
}

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.