我有两个不同的数组setOne
和setTwo
。我想创建一个包含10个元素的新数组,但前3个元素应该从第一个数组(setOne
)中随机出现,剩下的7个元素将来自setOne
和setTwo
。
这是数组:
var setOne = [['12+16','28'], ['15+11','26'], ['16+22','38'], ['13+14','27'], ['15+24','39'], ['14+12','26'], ['10+17','27'], ['11+11','22'],
['13+15','28'], ['12+21','33'], ['24+13','37'], ['33+21','54'], ['40+18','58'], ['34+31','65'], ['25+42','67'], ['22+15','37'],
['24+12','36'], ['20+15','35'], ['25+14','39'], ['21+21','42'], ['41+25','66'], ['53+24','77'], ['35+31','66'], ['62+37','99'],
['54+35','89']];
var setTwo = [['15+18=','33'], ['17+17=','34'], ['13+19=','32'], ['18+14=','32'], ['15+27=','42'], ['18+17=','35'], ['27+29=','56'], ['23+28=','51'],
['36+37=','73'], ['45+25=','70'], ['46+45=','91'], ['38+57=','95'], ['49+43=','92'], ['37+53=','90'], ['48+33=','81']];

generateQues: function() {
//Set position for set Questins
//quesTextValue = this.add.text(50,400,randQues[0], textStyleQues);
// Generating random questions from given list of ques - setOne.
for(var i = 0; i < 3; i++ ) {
if(i<=3) {
var ranNum = Math.floor(Math.random() * setOne.length);
//console.log(ranNum);
randQues = setOne[ranNum];
console.log(randQues);
// quesTextValue = this.add.text(50,400, randQues[0],textStyleQues);
} else {
var ranNum = Math.floor(Math.random() * setTwo.length);
var rque = setTwo[ranNum];
console.log(rque);
randQues.push(rque);
console.log(randQues);
}
}
// quesTextValue.destroy();
},
&#13;
请帮我解决这个问题。
答案 0 :(得分:0)
您可以使用一些随机值并拼接所选项目以防止结果集中出现重复。
var setOne = [['12+16', '28'], ['15+11', '26'], ['16+22', '38'], ['13+14', '27'], ['15+24', '39'], ['14+12', '26'], ['10+17', '27'], ['11+11', '22'], ['13+15', '28'], ['12+21', '33'], ['24+13', '37'], ['33+21', '54'], ['40+18', '58'], ['34+31', '65'], ['25+42', '67'], ['22+15', '37'], ['24+12', '36'], ['20+15', '35'], ['25+14', '39'], ['21+21', '42'], ['41+25', '66'], ['53+24', '77'], ['35+31', '66'], ['62+37', '99'], ['54+35', '89']],
setTwo = [['15+18=', '33'], ['17+17=', '34'], ['13+19=', '32'], ['18+14=', '32'], ['15+27=', '42'], ['18+17=', '35'], ['27+29=', '56'], ['23+28=', '51'], ['36+37=', '73'], ['45+25=', '70'], ['46+45=', '91'], ['38+57=', '95'], ['49+43=', '92'], ['37+53=', '90'], ['48+33=', '81']],
sets = [setOne.slice(), setTwo.slice()],
count = [3, 7],
result = [],
a, p;
while (count[0] + count[1]) {
p = Math.floor(Math.random() * 2);
if (count[p]) {
a = sets[p];
result.push(a.splice(Math.floor(Math.random() * a.length)[0], 1));
count[p]--;
}
}
console.log(result);
&#13;
答案 1 :(得分:0)
你可能喜欢这样;前三个是来自第一个数组的唯一随机选择,其余七个是来自两个数组中的一个的唯一随机选择。
var setOne = [['12+16','28'], ['15+11','26'], ['16+22','38'], ['13+14','27'], ['15+24','39'], ['14+12','26'], ['10+17','27'], ['11+11','22'], ['13+15','28'], ['12+21','33'], ['24+13','37'], ['33+21','54'], ['40+18','58'], ['34+31','65'], ['25+42','67'], ['22+15','37'], ['24+12','36'], ['20+15','35'], ['25+14','39'], ['21+21','42'], ['41+25','66'], ['53+24','77'], ['35+31','66'], ['62+37','99'], ['54+35','89']],
setTwo = [['15+18=','33'], ['17+17=','34'], ['13+19=','32'], ['18+14=','32'], ['15+27=','42'], ['18+17=','35'], ['27+29=','56'], ['23+28=','51'], ['36+37=','73'], ['45+25=','70'], ['46+45=','91'], ['38+57=','95'], ['49+43=','92'], ['37+53=','90'], ['48+33=','81']];
s1Copy = setOne.slice(),
result = [];
for (var i = 0; i < 3; i++) result.push(s1Copy.splice(~~(Math.random()*s1Copy.length),1)[0]);
s1Copy.push(...setTwo);
for (var i = 0; i < 7; i++) result.push(s1Copy.splice(~~(Math.random()*s1Copy.length),1)[0]);
console.log(result);
&#13;