0

I would like to generate a random value on an array. But each click, the array gain a new value, so I don't want to get a random value who have already a value on the array. In a nutshell, I need to random for the empty value of the array.

Here is a little part of the code. I have 9 values on my array, and at the beginning all of them are empty.

var gameCase = ['', '', '', '', '', '', '', '', ''];
var randomValue = gameCase[VALUE_EMPTY*][Math.round(Math.random()*gameCase.length)];

*Here is the part I don't really know how to do it.

2
  • So you want random integers with no repeated values. Correct? So an array like ['','',''] could be ['1','2','3'] but not ['1','1','2'] Commented Apr 13, 2013 at 15:45
  • I have my gameCase array is filled with a variable. Each click on a list item, the array gain another value. So I need to exclude this new key/value just add on the array and generate a new random value to add on this array. It's really strange to explain. Commented Apr 14, 2013 at 17:00

1 Answer 1

3

There are few ways to approach this:

Method 1: Keep generating random index until it points to an empty value

var gameCase = ['', '', '', '', '', '', '', '', ''];
var randomIndex = Math.round(Math.random()*gameCase.length) % emptyCases.length;
var randomValue = gameCase[randomIndex];
// while we haven't found the empty value
while (randomValue !== '') {
    // keep on looking
    randomIndex = Math.round(Math.random()*gameCase.length % emptyCases.length);
    randomValue = gameCase[randomIndex];
}
// when we exit the while loop:
//     - randomValue would === ''
//     - randomIndex would point to its position in gameCase[]

Method 2: Have a 2nd array that keeps track of which indices of gameCase array have empty values

var gameCase = ['', '', '', '', '', '', '', '', ''];
var emptyCases = [0,1,2,3,4,5,6,7,8];

if (emptyCases.length > 0) {
    // generate random Index from emptyCases[]
    var randomIndex = emptyCase[Math.round(Math.random()*emptyCase.length) % emptyCases.length];
    // get the corresponding value
    var randomValue = gameCase[randomIndex];
    // remove index from emptyCases[]
    emptyCases.splice(randomIndex, 1);
}

Method #2 is more efficient in a sense because you don't have waste time to generate/guess a random index. For Method #1 you need a way to check if there are any empty values left in gameCase[] or else you might be generating/guessing forever in an infinite loop.

More: When you set values for gameCase[] you need to update emptyCases[] accordingly to accurately reflect the state of gameCase[]:

var gameCase = ['', '', '', '', '', '', '', '', ''];
var emptyCases = [0,1,2,3,4,5,6,7,8];

/* Update a value in gameCase[] at the specified index */
var setGameCaseValue = function(index, value) {
    // set value for gameCase[]
    gameCase[index] = value;

    if (value !== '') {    // we're setting a value
        // remove that index from emptyCases[]
        emptyCases.splice(emptyCases.indexOf(index), 1);   
    } else {    // we're setting it back to empty string
        // add that index into emptyCases[] that refers to the empty string in gameCase[]
        emptyCases.push(index);        
    }
};

setGameCaseValue(2, 'null');
// gameCase now has ['','','null','','','','','','']
// emptyCases now has [0,1,3,4,5,6,7,8]

setGameCaseValue(0, 'null');
// gameCase now has ['null','','null','','','','','','']
// emptyCases now has [1,3,4,5,6,7,8]

setGameCaseValue(5, 'null');
// gameCase now has ['null','','null','','','null','','','']
// emptyCases now has [1,3,4,6,7,8]

setGameCaseValue(7, 'null');
// gameCase now has ['null','','null','','','null','','null','']
// emptyCases now has [1,3,4,6,8]

See fiddle: http://jsfiddle.net/rWvnW/1/

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

5 Comments

Very good answer. Nice of you to take the time to explain the solutions, their relative merits and why one is better then the other. Got my vote!
I think you forget one "s" to the emptyCase(s) words on the method 2. Otherwise, I tried the two methods, but I don't really succes to integrate them at all. When I did a console.log() of the randomeValue nothing appears.
I think something went wrong. When I tried to add some value to my gameCase array, the emptyCases doesn't change jsfiddle.net/rWvnW
Humm, just a question, what it the interest to create a variable randomValue ? I tried to console.log() the render but nothing appears.
@Jeremdsgn No particular reason, you can exclude it if you don't plan on using that variable.

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.