Skip to content

Commit 0008b6c

Browse files
committed
Better random shuffle for cases
1 parent df9027e commit 0008b6c

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

scripts/main.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,12 +1237,23 @@ function generateTrainCaseList() {
12371237
if (trainCaseListTemp.length == 0) ELEM_SCRAMBLE.innerHTML = "no case selected";
12381238

12391239
// Randomize Cases
1240-
trainCaseListTemp.sort(() => Math.random() - 0.5);
1240+
shuffleArray(trainCaseListTemp);
12411241

12421242
// Add new cases to current list
12431243
trainCaseList = trainCaseList.concat(trainCaseListTemp);
12441244
}
12451245

1246+
/**
1247+
* https://stackoverflow.com/a/12646864
1248+
* Less biased shuffle
1249+
*/
1250+
function shuffleArray(array) {
1251+
for (let i = array.length - 1; i > 0; i--) {
1252+
const j = Math.floor(Math.random() * (i + 1));
1253+
[array[i], array[j]] = [array[j], array[i]];
1254+
}
1255+
}
1256+
12461257
/**
12471258
* Show next/previous case
12481259
* Called when user clicks on Previous/Next Scramble button or presses space

0 commit comments

Comments
 (0)