Created
January 27, 2022 14:01
-
-
Save triacontane/a6a2a1d5b40acc8be71bcb3e84dd6ef0 to your computer and use it in GitHub Desktop.
ソート関数を使ったシャッフル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
この方法には二つの問題があります。
ひとつはシャッフル結果が偏るという点です。
https://www.delftstack.com/ja/howto/javascript/shuffle-array-javascript/
もうひとつは、compareFunctionのルールである「与えられた引数が同じなら返値も同じでなければならない」に反している点です。
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/sort