简体   繁体   English

Fisher-Yates 如何打乱 JavaScript 数组?

[英]How to Fisher-Yates shuffle a JavaScript array?

I'm trying to modify this quiz app from a tutorial from Awais Mirza我正在尝试从Awais Mirza的教程中修改此测验应用程序

I would like to pick a random selection of questions from a master array and push it in to a selection array the script uses to populate questions, so the quiz will give a random set of questions form the master array every time the quiz is run.我想从主数组中随机选择问题并将其推送到脚本用于填充问题的选择数组中,因此每次测验运行时,测验都会从主数组中给出一组随机问题。 I thought i could use Fisher-Yates shuffle to randomize the master array before pushing the selected number of questions into a selection array.我想我可以使用 Fisher-Yates shuffle 在将选定数量的问题推送到选择数组之前随机化主数组。

Why does the Fisher-Yates shuffle work with this array;为什么 Fisher-Yates shuffle 对这个数组起作用;

 var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]; var i = arr.length, j, temp; while(--i > 0){ j = Math.floor(Math.random()*(i+1)); temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } console.log(arr);

but not with this array?但不是这个数组?

var Questions = [
    new Question("What comes after 1?", ["1", "2","3", "4"], "2"),
    new Question("What comes after 2?", ["1", "2", "3", "4"], "3"),
    new Question("What comes after 3?", ["1", "2", "3", "4"], "4")
];

The Fisher-Yates algorithm works exclusively with array indexes so you don't need different implementations depending on array contents . Fisher-Yates 算法仅适用于数组索引,因此您不需要根据数组内容进行不同的实现

To illustrate that I've moved the sorting code to a function so it can be reused:为了说明我已将排序代码移到一个函数中,以便可以重用:

 function shuffle(arr) { var i = arr.length, j, temp; while(--i > 0){ j = Math.floor(Math.random()*(i+1)); temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } } class Question { constructor(title, options, solution) { this.title = title; this.options = options; this.solution = solution; } } var integerArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]; var questions = [ new Question("What comes after 1?", ["1", "2","3", "4"], "2"), new Question("What comes after 2?", ["1", "2", "3", "4"], "3"), new Question("What comes after 3?", ["1", "2", "3", "4"], "4") ]; shuffle(integerArray); shuffle(questions); console.log(integerArray, questions);

Could be a tad easier with randojs.com , which uses this method of shuffling, but it's completely preference.使用randojs.com可能会更容易一些,它使用这种洗牌方法,但这完全是偏好。

 function Question(question, choices, answer){ this.question = question; this.choices = choices; this.answer = answer; } // create questions var questions = [ new Question("Which one is not an object oriented programming language?", ["Java", "C#","C++", "C"], "C"), new Question("Which language is used for styling web pages?", ["HTML", "JQuery", "CSS", "XML"], "CSS"), new Question("There are ____ main components of object oriented programming.", ["1", "6","2", "4"], "4"), new Question("Which language is used for web apps?", ["PHP", "Python", "Javascript", "All"], "All"), new Question("MVC is a ____.", ["Language", "Library", "Framework", "All"], "Framework") ]; // Shuffle array var sequence = randoSequence(questions); sequence.forEach((item, i) => {sequence[i] = item.value;}); // choose 3 first questions from shuffeled array var selection = sequence.slice(0, 3); console.log(selection);
 <script src="https://randojs.com/1.0.0.js"></script>

I sloved it with this:我用这个sloved它:

// create questions
var questions = [
    new Question("Which one is not an object oriented programming language?", ["Java", "C#","C++", "C"], "C"),
    new Question("Which language is used for styling web pages?", ["HTML", "JQuery", "CSS", "XML"], "CSS"),
    new Question("There are ____ main components of object oriented programming.", ["1", "6","2", "4"], "4"),
    new Question("Which language is used for web apps?", ["PHP", "Python", "Javascript", "All"], "All"),
    new Question("MVC is a ____.", ["Language", "Library", "Framework", "All"], "Framework")
];

// Shuffle array
var arr = questions;
var i = arr.length, j, temp;
while(--i > 0){
    j = Math.floor(Math.random()*(i+1));
    temp = arr[j];
    arr[j] = arr[i];
    arr[i] = temp;
}

// choose 3 first questions from shuffeled array
var selection = arr.slice(0, 3);

// create quiz
var quiz = new Quiz(selection);

// display quiz
populate();

Thank you ever so much for your help :)非常感谢您的帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM