[英]Random value on an array
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.
我的数组上有9个值,一开始它们都是空的。
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. *这是我不知道该怎么做的部分。
There are few ways to approach this: 有几种方法可以解决此问题:
Method 1: Keep generating random index until it points to an empty value 方法1:继续生成随机索引,直到它指向一个空值
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 方法2:使用第二个数组来跟踪
gameCase
数组的哪些索引具有空值
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. 从某种意义上讲,方法2更有效,因为您没有浪费时间来生成/猜测随机索引。 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. 对于方法1,您需要一种方法来检查
gameCase[]
是否还有任何空值,否则您可能会无限循环地永远生成/猜测。
More: When you set values for gameCase[]
you need to update emptyCases[]
accordingly to accurately reflect the state of gameCase[]
: 更多:在为
gameCase[]
设置值时,您需要相应地更新emptyCases[]
以准确反映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/ 请参阅小提琴: http : //jsfiddle.net/rWvnW/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.