Interesting points. Let's check them out and see how they fare.
Note: I will be using this Fisher-Yates implementation which I got from
@bass2yang
JavaScript:
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
Performance
Here are the two methods I use for running the two types of shuffles a bunch of times:
JavaScript:
function shuffleArrayFisherYates(start, times) {
array = start.clone();
for (let t = 0; t < times; t++) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
function shuffleArrayViaSort(start, times) {
array = start.clone();
for (let t = 0; t < times; t++) {
array.sort(() => Math.random() - 0.5);
}
}
I open up the DevTools when running an RPG Maker MV project start recording
Then I run the following code in the console, after which I stop the profiling.
$array = [1,2,3,4,5,6];
times = 1000000;
shuffleArrayFisherYates($array, times);
shuffleArrayViaSort($array, times);
The resulting running time were:
Fisher-Yates:
67ms
Sync sort:
332ms
I repeated the experiement a couple of times (each with an array of 6 elements) and the outcome was consistent with Sync being 5 times slower than Fisher-Yates.
Notice that we are shuffling a million times. This means that we can expect the actual time save of switching from Sync sort to Fisher-Yates to be less than 1 microsecond, a neglible difference.
In conclusion, in our context of a single shuffle of 6 elements, both
Sync sort and
Fisher-Yates are fast enough.
Bias
Biases can have many forms. To help see how the bias look, I count up which position each element end up getting. Here's my code for that:
JavaScript:
function shuffleSyncBiasTest(start, times) {
counts = [];
for (let t = 0; t < times; t++) {
array = start.clone();
array.sort(() => Math.random() - 0.5);
for (let i = 0; i < array.length; i++)
{
var element = array[i];
if (counts[element] === undefined)
{
counts[element] = [];
}
var count = counts[element];
if (count[i] === undefined)
{
count[i] = 1;
}
else
{
count[i]++;
}
}
}
return counts;
}
function shuffleFisherYates(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
function shuffleFisherYatesBiasTest(start, times) {
counts = [];
for (let t = 0; t < times; t++) {
array = start.clone();
shuffleFisherYates(array);
for (let i = 0; i < array.length; i++)
{
var element = array[i];
if (counts[element] === undefined)
{
counts[element] = [];
}
var count = counts[element];
if (count[i] === undefined)
{
count[i] = 1;
}
else
{
count[i]++;
}
}
}
return counts;
}
I run each shuffle a million times again:
shuffleSyncBiasTest($array, 1000000);
shuffleFisherYatesBiasTest($array, 1000000);
Here is my results after I put them into Excel, divide by a million and use the percentage number format:
Spoiler: Raw Data
shuffleSyncBiasTest($array, 1000000);
[298158, 297318, 199273, 113738, 60488, 31025]
[298938, 298364, 197801, 113258, 60411, 31228]
[198086, 199203, 241004, 185371, 113682, 62654]
[113432, 113408, 185531, 259970, 202438, 125221]
[60077, 60649, 114085, 203092, 312653, 249444]
[31309, 31058, 62306, 124571, 250328, 500428]
shuffleFisherYatesBiasTest($array, 1000000);
[166651, 167229, 166820, 166311, 166322, 166667]
[167235, 166061, 166448, 166895, 166643, 166718]
[165670, 166609, 166984, 166782, 167467, 166488]
[166613, 166497, 166212, 167002, 166468, 167208]
[166677, 166598, 167500, 166240, 166595, 166390]
[167154, 167006, 166036, 166770, 166505, 166529]
This is a really interesting result. Fisher-Yates has this nice uniform distribution whereas the earlier numbes in the Sync shuffle has a tendency to be placed earlier in the resulting array. Both can make sense depending on how you want to design the randomness in your game.
Based on this information I would use Fisher-Yates as the standard option. I may use the Sync shuffle where I specifically want to skew the distribution in the illustrated way.
Neither of these options have that much control though. For more control I recommend searching for how to implement a weighted shuffle.
Note: Nothing here is truly random. Not that true randomness really matters that much in the vast majority of games.