如何随机排列 javascript 中的列表列表
How to shuffle a list of lists in javascript
假设我在 Javascript 中有一个列表列表,如下所示:
var list_of_list = [["N", "B"], ["E", "B"], ["Y", "R"], ["R", "R"], ["A", "B"], ["U", "R"]]
我想随机化列表的一级排序而不随机化列表内部。所以,例如,
shuffle(list_of_lists)
> [["E", "B"], ["R", "R"], ["Y", "R"], ["N", "B"], ["U", "R"], ["A", "B"]]
是一种可能的随机洗牌,因为它在随机化顶级列表的同时保留了嵌套列表的顺序。
我尝试了多种不同的方法,但似乎无法进行改组。例如,none 这些洗牌工作的标准方法:
var list_of_list = [["N", "B"], ["E", "B"], ["Y", "R"], ["R", "R"], ["A", "B"], ["U", "R"]]
function shuffle(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
function shuffle_2(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
list_of_list2 = list_of_list.sort(func);
function func(a, b) {
return 0.5 - Math.random();
}
console.log(list_of_list2.join("|"));
console.log(shuffle(list_of_list).join("|"));
console.log(shuffle_2(list_of_list).join("|"));
正确的做法是什么?
var list_of_list = [["N", "B"], ["E", "B"], ["Y", "R"], ["R", "R"], ["A", "B"], ["U", "R"]];
function shuffle(a){
const arrlength = a.length;
let shuffled = []; //array to be returned
let numsused = []; //numbers chosen by rand
while(shuffled.length < arrlength){
let newnum = false;
while(!newnum){
const randm = Math.floor(Math.random() * arrlength);
if(numsused.indexOf(randm) === -1){
shuffled.push(a[randm]);
newnum = true;
numsused.push(randm);
}
}
}
return shuffled;
}
console.log(shuffle(list_of_list));
假设我在 Javascript 中有一个列表列表,如下所示:
var list_of_list = [["N", "B"], ["E", "B"], ["Y", "R"], ["R", "R"], ["A", "B"], ["U", "R"]]
我想随机化列表的一级排序而不随机化列表内部。所以,例如,
shuffle(list_of_lists)
> [["E", "B"], ["R", "R"], ["Y", "R"], ["N", "B"], ["U", "R"], ["A", "B"]]
是一种可能的随机洗牌,因为它在随机化顶级列表的同时保留了嵌套列表的顺序。
我尝试了多种不同的方法,但似乎无法进行改组。例如,none 这些洗牌工作的标准方法:
var list_of_list = [["N", "B"], ["E", "B"], ["Y", "R"], ["R", "R"], ["A", "B"], ["U", "R"]]
function shuffle(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
function shuffle_2(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
list_of_list2 = list_of_list.sort(func);
function func(a, b) {
return 0.5 - Math.random();
}
console.log(list_of_list2.join("|"));
console.log(shuffle(list_of_list).join("|"));
console.log(shuffle_2(list_of_list).join("|"));
正确的做法是什么?
var list_of_list = [["N", "B"], ["E", "B"], ["Y", "R"], ["R", "R"], ["A", "B"], ["U", "R"]];
function shuffle(a){
const arrlength = a.length;
let shuffled = []; //array to be returned
let numsused = []; //numbers chosen by rand
while(shuffled.length < arrlength){
let newnum = false;
while(!newnum){
const randm = Math.floor(Math.random() * arrlength);
if(numsused.indexOf(randm) === -1){
shuffled.push(a[randm]);
newnum = true;
numsused.push(randm);
}
}
}
return shuffled;
}
console.log(shuffle(list_of_list));