Skip to content

Instantly share code, notes, and snippets.

@triacontane
Created January 27, 2022 14:01
Show Gist options
  • Save triacontane/a6a2a1d5b40acc8be71bcb3e84dd6ef0 to your computer and use it in GitHub Desktop.
Save triacontane/a6a2a1d5b40acc8be71bcb3e84dd6ef0 to your computer and use it in GitHub Desktop.
ソート関数を使ったシャッフル
const list = [1,3,4,9,11,13,14,22,30,40];
list.sort((a, b) => Math.random() - 0.5);
list.forEach((value, index) => {
$gameVariables.setValue(index + 1, value);
});
@triacontane
Copy link
Author

triacontane commented Jan 27, 2022

この方法には二つの問題があります。

ひとつはシャッフル結果が偏るという点です。
https://www.delftstack.com/ja/howto/javascript/shuffle-array-javascript/

もうひとつは、compareFunctionのルールである「与えられた引数が同じなら返値も同じでなければならない」に反している点です。
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment