戻る

配列要素をランダムに入れ替え
フィッシャー・イェーツのシャッフルアルゴリズムを使用した配列の並び替え

[サンプル]

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]];
要素の交換をする処理です。
1回目乱数のindex:2
i(インデックスの値:4)とj(インデックスの値:2)に入っている値を交換します。
index変更前 変更後
01 1
12 2
235
34 4
453
2回目乱数のindex:2
i(インデックスの値:3)とj(インデックスの値:2)に入っている値を交換します。
index変更前 変更後
01 1
12 2
254
345
43 3
3回目乱数のindex:2
i(インデックスの値:2)とj(インデックスの値:2)に入っている値を交換します。
しかし、同じインデックスの交換となるため、データ上としては変更ありません。
index変更前 変更後
01 1
12 2
244
35 5
43 3
4回目乱数のindex:0
i(インデックスの値:1)とj(インデックスの値:0)に入っている値を交換します。
index変更前 変更後
012
121
24 4
35 5
43 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 timeRandom number index:2
Swap the values in i (index value:4) and j (index value:2).
indexBefore After
01 1
12 2
235
34 4
453
Second TimeRandom Number Index: 2
Swap the values in i (index value: 3) and j (index value: 2).
indexBefore Change After Change
01 1
12 2
254
345
43 3
3rd timeRandom 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.
indexBefore After
01 1
12 2
244
35 5
43 3
4th timeRandom number index:0
Swap the values in i (index value: 1) and j (index value: 0).
indexBefore After
012
121
24 4
35 5
43 3

The fifth time, there will be no replacement, so the item will not be processed.


back



著作権情報
ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。
ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、
または公開したりすることはできません。
当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、
その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について
一切の責任を負わないものとします。