戻る| 配列要素をランダムに入れ替え |
| フィッシャー・イェーツのシャッフルアルゴリズムを使用した配列の並び替え |
[サンプル]
copy
function shuffleArray(array)
{
for (let i = array.length - 1; i > 0; i--)
{
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
//[使用例]
const array = [1, 2, 3, 4, 5];
const shuffledArray = shuffleArray(array);
console.log(shuffledArray); //出力例:[2, 1, 4, 5, 3] (毎回ランダムに並び替わります)
for (let i = array.length - 1; i > 0; i--)
最後の配列要素から要素の1個手前まで降順に処理をします。
const j = Math.floor(Math.random() * (i + 1));
[Math.random]
0以上1未満の浮動小数点(小数点以下を含む数字)の疑似乱数を生成します。
配列個数は5個でインデックスの範囲は0から4までです。
i = array.length - 1
i = 5 - 1
i = 4
Math.floor(小数点切り捨て)で使用するため、
範囲内の4をコール可能とするため+1をした値を乱数生成の範囲終了値とします。
[array[i], array[j]] = [array[j], array[i]];
要素の交換をする処理です。
i(インデックスの値:4)とj(インデックスの値:2)に入っている値を交換します。
| index | 変更前 | | 変更後 |
| 0 | 1 | | 1 |
| 1 | 2 | | 2 |
| 2 | 3 | → | 5 |
| 3 | 4 | | 4 |
| 4 | 5 | → | 3 |
i(インデックスの値:3)とj(インデックスの値:2)に入っている値を交換します。
| index | 変更前 | | 変更後 |
| 0 | 1 | | 1 |
| 1 | 2 | | 2 |
| 2 | 5 | → | 4 |
| 3 | 4 | → | 5 |
| 4 | 3 | | 3 |
i(インデックスの値:2)とj(インデックスの値:2)に入っている値を交換します。
しかし、同じインデックスの交換となるため、データ上としては変更ありません。
| index | 変更前 | | 変更後 |
| 0 | 1 | | 1 |
| 1 | 2 | | 2 |
| 2 | 4 | → | 4 |
| 3 | 5 | | 5 |
| 4 | 3 | | 3 |
i(インデックスの値:1)とj(インデックスの値:0)に入っている値を交換します。
| index | 変更前 | | 変更後 |
| 0 | 1 | → | 2 |
| 1 | 2 | → | 1 |
| 2 | 4 | | 4 |
| 3 | 5 | | 5 |
| 4 | 3 | | 3 |
5回目は交換するものがないため処理しません。
戻る back| Randomly swap array elements |
| Reordering an array using the Fisher-Yates shuffle algorithm |
[sample]
copy
function shuffleArray(array)
{
for (let i = array.length - 1; i > 0; i--)
{
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
//[Usage example]
const array = [1, 2, 3, 4, 5];
const shuffledArray = shuffleArray(array);
console.log(shuffledArray); //Example output:[2, 1, 4, 5, 3] (Randomly rotated each time)
for (let i = array.length - 1; i > 0; i--)
Processes elements in descending order from the last array element to the element immediately before it.
const j = Math.floor(Math.random() * (i + 1));
[Math.random]
Generates pseudo-random floating-point numbers (numbers with decimal points) between 0 and 1.
The array contains 5 elements, with indexes ranging from 0 to 4.
i = array.length - 1
i = 5 - 1
i = 4
This is used with Math.floor (decimal points truncated),
The end value of the random number generation range is increased by 1 to allow for the value 4 within the range.
[array[i], array[j]] = [array[j], array[i]];
This is the element swap process.
| First time | Random number index:2 |
Swap the values in i (index value:4) and j (index value:2).
| index | Before | | After |
| 0 | 1 | | 1 |
| 1 | 2 | | 2 |
| 2 | 3 | → | 5 |
| 3 | 4 | | 4 |
| 4 | 5 | → | 3 |
| Second Time | Random Number Index: 2 |
Swap the values in i (index value: 3) and j (index value: 2).
| index | Before Change | | After Change |
| 0 | 1 | | 1 |
| 1 | 2 | | 2 |
| 2 | 5 | → | 4 |
| 3 | 4 | → | 5 |
| 4 | 3 | | 3 |
| 3rd time | Random number index: 2 |
Swap the values in i (index value: 2) and j (index value: 2).
However, since the same index is swapped, the data remains unchanged.
| index | Before | | After |
| 0 | 1 | | 1 |
| 1 | 2 | | 2 |
| 2 | 4 | → | 4 |
| 3 | 5 | | 5 |
| 4 | 3 | | 3 |
| 4th time | Random number index:0 |
Swap the values in i (index value: 1) and j (index value: 0).
| index | Before | | After |
| 0 | 1 | → | 2 |
| 1 | 2 | → | 1 |
| 2 | 4 | | 4 |
| 3 | 5 | | 5 |
| 4 | 3 | | 3 |
The fifth time, there will be no replacement, so the item will not be processed.
back