我使用此函数从数组中获取随机值:
game.colors = [red,green,blue,yellow];
game.computerMoves = [];
function random () {
let moves = game.colors[Math.floor(Math.random() * game.colors.length)]
game.computerMoves.push(moves);
return game.computerMoves;
}
但是我希望能够从这个函数中获得不同数量的随机值,例如,如果我使用一个参数从数组中获取6个随机值或者我可以更改的9个随机值。我怎样才能做到这一点?现在例如,如果我想要9个随机值,我将需要激活此函数9次才能获得它们,但我希望能够通过激活此函数一次来获取它们。
答案 0 :(得分:0)
只需创建另一个调用随机数发生器所需次数的函数:
const game = {
colors: ['red', 'green', 'blue', 'yellow'],
computerMoves: []
}
function getRandomMove() {
return game.colors[Math.floor(Math.random() * game.colors.length)];
}
function getRandomMoves(randomMoveCount) {
const generatedRandomMoves = Array.from({
length: randomMoveCount
}, getRandomMove);
game.computerMoves = [...game.computerMoves, ...generatedRandomMoves];
return game.computerMoves;
}
console.log(getRandomMoves(5));

答案 1 :(得分:0)
这个怎么样?
var randomSet = []
function random (num) {
for(var i = 0 ; i < num ; i++){
let moves = game.colors[Math.floor(Math.random() * game.colors.length)]
game.computerMoves.push(moves);
}
randomSet = game.computerMoves;
//or return game.computerMoves
}
您可以使用全局变量randomSet在将来使用
答案 2 :(得分:0)
您只需要将let moves
和push()
放在从for
到0
的{{1}}循环内,并传递给函数中的数字:
game = {}; // Added
game.colors = ['red', 'green', 'blue', 'yellow'];
game.computerMoves = [];
function random(amount) {
for (let i = 0; i < amount; i++) {
let moves = game.colors[Math.floor(Math.random() * game.colors.length)]
game.computerMoves.push(moves);
}
return game.computerMoves;
}
console.log(random(9));
&#13;
答案 3 :(得分:0)
如果可以重复项目,那么
const rand = (x = 1) =>
Math.floor (Math.random () * x)
const sample = (xs = [], n = 1) =>
n === 0
? []
: [ xs [rand (xs.length)], ...sample (xs, n - 1) ]
const colors =
[ 'red', 'green', 'blue', 'yellow' ]
console.log (sample (colors, 2))
// [ 'green', 'blue' ]
console.log (sample (colors, 3))
// [ 'yellow', 'blue', 'blue' ]
console.log (sample (colors, 4))
// [ 'green', 'red', 'red', 'yelow' ]
&#13;
如果不能重复采样元素......
const pick = (xs, i) =>
[ xs [i], xs.slice(0,i).concat(xs.slice(i + 1)) ]
const rand = (x = 1) =>
Math.floor (Math.random () * x)
const sample = (xs = [], n = 1) =>
{
if (xs.length === 0 || n === 0)
return []
const [ x, next ] =
pick (xs, rand (xs.length))
return [ x, ...sample (next, n - 1) ]
}
const colors =
[ 'red', 'green', 'blue', 'yellow' ]
console.log (sample (colors, 3))
// [ 'red', 'blue', 'yellow' ]
console.log (sample (colors, 4))
// [ 'blue', 'green', 'yellow', 'red' ]
console.log (sample (colors, 5))
// [ 'green', 'blue', 'red', 'yelow' ]
&#13;
答案 4 :(得分:0)
此代码段可以帮助您从给定数组中随机选择N个项目:
let pick = (arr, count = 1) => {
let _arr = [...arr];
_arr.sort(() => {
return Math.random() - 0.5;
});
let len = _arr.length;
let total = Math.max(Math.min(count, len), 1);
return (total >= len) ? _arr : _arr.splice(0, total);
};
例如:
let a = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let i = 0;
while (i < 100) {
let b = pick(a, 3);
console.log(b);
i++;
}
答案 5 :(得分:0)
在演示中输入1到999之间的数字,然后单击: 模式。详情在演示中进行了评论。
/* randomMove() is the callback function
|| Get the number from user input.qty
|| This number determines number of loops.
|| A utility function, shuffle, based on the Fisher|Yates
|| algorithm is ran on the colors array.
|| Dispite that, there were too many consecutive repeats.
|| shuffle() is now modified to use a timestamp and an OR Gate.
*/
function randomMove(e) {
var qty = parseInt(document.getElementById('qty').value, 10);
var colors = ['red', 'green', 'blue', 'yellow'];
var SimonMoves = [];
for (let i = 0; i < qty; i++) {
var moves = shuffle(colors);
SimonMoves.push(moves);
}
console.log(SimonMoves);
return SimonMoves;
}
document.querySelector('.btn').addEventListener('click', randomMove, false);
function shuffle(array) {
var i = 0;
var j = 0;
var stamp = performance.now();
var temp = null;
for (i = array.length - 1; i > 0; i -= 1) {
j = Math.floor(Math.random() * (i + 1));
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
var flip = stamp % 2 === 0 ? array[i] : array[j];
return flip;
}
input {
width: 12ch;
font: inherit
}
[type=number] {
text-align: right;
width: 5ch
}
<input id='qty' type='number' min='1' max='999'>
<input class='btn' type='button' value='PATTERN'>