I am trying to create a function that will generate a random number and store it in an array so the first click will send the random number to the index[0] click 2 to index [1] ect. I need to be able to compare the number with the one before (index [4] with index [3].I am sure the answer is right in front of me but i cannot find a solution. Any help would be fantastic
我正在尝试创建一个函数,该函数将生成一个随机数并将其存储在一个数组中,因此第一次单击将将随机数发送到索引[0],单击2以索引[1]。我需要能够比较这个数字和之前的一个(索引[4]和索引[3]。我肯定答案就在眼前,但我找不到解决办法。任何帮助都是不可思议的。
for(i = 0;i < 12;i++) {
var random_number = Math.floor(Math.random() * 12);
var myArray = [];
myArray.push(random_number);
console.log(myArray.length);
document.getElementById("catchme").innerHTML = random_number;
}
});
http://codepen.io/kingnarwal/pen/BzjRjq?editors=1111
http://codepen.io/kingnarwal/pen/BzjRjq?editors=1111
1 个解决方案
#1
0
var myArray = [];
for(var x = 0, maxValue = 12, random_number; x < 12; x++) {
do {
random_number = Math.floor(Math.random() * maxValue );
} while(random_number == myArray[x - 1]);//Check if the number is the same value
myArray.push(random_number);
}
console.log(myArray);
This does not generate an array with random unique numbers since you're only checking the item before the current item.
这不会生成一个具有随机数的数组,因为您只在当前项之前检查项目。
To make values unique in whole array:
使值在整个数组中是唯一的:
var myArray = [];
for(var x = 0, maxValue = 12; x < maxValue; x++) {
myArray.splice(Math.floor(Math.random() * myArray.length), 0, x);
}
console.log(myArray);
Above is a bit hackish method since it uses splice with an random index :P Keep in mind that above method is FAR from random.
以上是一种有点粗糙的方法,因为它使用了一个随机索引:P记住上面的方法不是随机的。
A more random method would be:
一种更随机的方法是:
var myArray = [];
for(var x = 0, x < 12; x++) {
myArray.push(x);
}
shuffle(myArray);
console.log(myArray);
You can use an array shuffle method from here: How to randomize (shuffle) a JavaScript array?
您可以在这里使用数组洗牌方法:如何随机化(shuffle)一个JavaScript数组?
#1
0
var myArray = [];
for(var x = 0, maxValue = 12, random_number; x < 12; x++) {
do {
random_number = Math.floor(Math.random() * maxValue );
} while(random_number == myArray[x - 1]);//Check if the number is the same value
myArray.push(random_number);
}
console.log(myArray);
This does not generate an array with random unique numbers since you're only checking the item before the current item.
这不会生成一个具有随机数的数组,因为您只在当前项之前检查项目。
To make values unique in whole array:
使值在整个数组中是唯一的:
var myArray = [];
for(var x = 0, maxValue = 12; x < maxValue; x++) {
myArray.splice(Math.floor(Math.random() * myArray.length), 0, x);
}
console.log(myArray);
Above is a bit hackish method since it uses splice with an random index :P Keep in mind that above method is FAR from random.
以上是一种有点粗糙的方法,因为它使用了一个随机索引:P记住上面的方法不是随机的。
A more random method would be:
一种更随机的方法是:
var myArray = [];
for(var x = 0, x < 12; x++) {
myArray.push(x);
}
shuffle(myArray);
console.log(myArray);
You can use an array shuffle method from here: How to randomize (shuffle) a JavaScript array?
您可以在这里使用数组洗牌方法:如何随机化(shuffle)一个JavaScript数组?