数学随机查找数组中的名称,而无需重复 [英] Math random to find Names in an Array while not duplicating
本文介绍了数学随机查找数组中的名称,而无需重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我想从一个名称数组中找到三个名称,然后将它们写到一个新的数组中(虽然到目前为止还没有得到),但是我遇到的问题是它一直在随机化我已经使用的相同名称找到。
查看 jsfiddle脚本。 / p>
代码:
findStudentBtn.onclick = findStudent;
函数findStudent(){
var studArray = [ John, Daniel, Hans, Lars, Tom, Amanda, Jane, Sarah] // 1-8
for(i = 0; i< 3; i ++){
if(i = 1){
var randomStud1 = studArray [Math.floor(studArray) .length * Math.random())];
msg8.innerHTML + = randomStud1 +,;
}
if(i = 2){
var randomStud2 = studArray [Math.floor(studArray.length * Math.random())];
msg8.innerHTML + = randomStud2 +,;
}
if(i = 3){
var randomStud3 = studArray [Math.floor(studArray.length * Math.random())];
msg8.innerHTML + = randomStud3 +,;
}
if(randomStud1 == randomStud2 || randomStud2 == randomStud3 || randomStud1 == randomStud3){
ms8.innerHTML =;
findStudent();
}
}
}
解决方案
对于对解决方案感兴趣但不愿意使用jsfidle的人,这是我使用的脚本。
findStudentBtn.onclick = function(){
findStudent();
};
var r_stud = [ John, Daniel, Hans, Lars, Tom, Amanda, Jane, Sarah];
var r_rndm = [];
var r_newr = [];
函数getStudent(){
//如果可用人数很少,则可以达到3。
if(r_rndm.length == r_stud。 length){
//重置随机选择的数组
r_rndm = [];
return r_newr;
}
//选择一个学生
var i_randomStudent = Math.floor(r_stud.length * Math.random());
var s_randomStudent = r_stud [i_randomStudent];
if(r_rndm.indexOf(s_randomStudent)> = 0){
//如果已全部选择,请重试。
getStudent();
} else {
//将学生添加到新选择和所有选择中
r_rndm.push(s_randomStudent);
r_newr.push(s_randomStudent);
//返回新数组
return r_newr;
}
};
函数findStudent(){
//重置新数组。
r_newr = [];
for(i = 0; i< 3; i ++){
getStudent();
};
//将新数组的内容打印到控制台。
console.log(r_newr);
for(i = 0; i< r_newr.length; i ++){
分隔符=(i == r_newr.length-1&& r_rndm.length == 0) ? ’’:‘,’;
msg8.innerHTML + = r_newr [i] +分隔符;
}
};
So I want to find three names out of an array of names which I then want to write to a new array (not gotten this far yet though), but the problem I have is that it keeps randomizing the same names I already found.
Check out the jsfiddle script.
Code:
findStudentBtn.onclick = findStudent;
function findStudent() {
var studArray = ["John","Daniel","Hans","Lars","Tom","Amanda","Jane","Sarah"] //1-8
for (i=0; i<3; i++) {
if (i=1) {
var randomStud1 = studArray[Math.floor(studArray.length * Math.random())];
msg8.innerHTML += randomStud1 + ", ";
}
if (i=2) {
var randomStud2 = studArray[Math.floor(studArray.length * Math.random())];
msg8.innerHTML += randomStud2 + ", ";
}
if (i=3) {
var randomStud3 = studArray[Math.floor(studArray.length * Math.random())];
msg8.innerHTML += randomStud3 + ", ";
}
if (randomStud1 == randomStud2 || randomStud2 == randomStud3 || randomStud1 == randomStud3){
ms8.innerHTML = "";
findStudent();
}
}
}
解决方案
For anyone interested in the solution, but not willing to go to the jsfidle, here's is the script I used.
findStudentBtn.onclick = function(){
findStudent();
};
var r_stud = ["John","Daniel","Hans","Lars","Tom","Amanda","Jane","Sarah"];
var r_rndm = [];
var r_newr = [];
function getStudent(){
//if the amount of available people, is to small to reach 3.
if(r_rndm.length == r_stud.length){
//reset the random selected array
r_rndm = [];
return r_newr;
}
//select a student
var i_randomStudent = Math.floor(r_stud.length * Math.random());
var s_randomStudent = r_stud[i_randomStudent];
if(r_rndm.indexOf(s_randomStudent) >= 0){
//if it's allready been selected, try again.
getStudent();
}else{
//add students to new selection and to alltime selection
r_rndm.push(s_randomStudent);
r_newr.push(s_randomStudent);
//return the new array
return r_newr;
}
};
function findStudent(){
//reset new array.
r_newr = [];
for(i = 0; i < 3; i++){
getStudent();
};
//print contents of new array to console.
console.log(r_newr);
for(i = 0; i < r_newr.length; i++){
separator = (i == r_newr.length - 1 && r_rndm.length == 0 ) ? '' : ', ';
msg8.innerHTML += r_newr[i] + separator;
}
};
这篇关于数学随机查找数组中的名称,而无需重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文