배열을 랜덤 화하기 위해 knuth shuffle을 사용하고 있습니다. 다른 배열을 추가하고 같은 방법으로 임의화할 수 있기를 바랍니다. 이전에 ['A|1|I,B|2|II,C|3|III,D|4|IV']
등의 문자열 내에서 배열을 분리하는 생각을했습니다.같은 방식으로 Knuth Shuffle 다중 배열
stackoverflow에서 this을 읽었지 만 knuth shuffle을 정렬하는 방법을 배울 수 없습니다.
위의 링크에서 추가 코드를 가져 와서 첫 번째 배열에서 문자열을 가져 오는 JS를 사용하고 있습니다. 귀하의 링크에서
Array.prototype.knuthShuffle = function()
{
var i = this.length, j, temp;
while (--i)
{
j = Math.floor(Math.random() * (i - 1));
temp = this[i];
this[i] = this[j];
this[j] = temp;
}
};
var array_chromatic = ['A', 'A%23', 'Bb', 'B', 'C', 'C%23', 'Db', 'D', 'D%23', 'Eb', 'E', 'F', 'F%23', 'Gb', 'G', 'G%23', 'Ab'],
array_chronumb = ['Aa', 'A%23a', 'Bbb', 'Bb', 'Cc', 'C%23c', 'Dbd', 'Dd', 'D%23d', 'Ebe', 'Ee', 'Ff', 'F%23f', 'Gbg', 'Gg', 'G%23g', 'Aba'];
function renderKnuth()
{
array_chromatic.knuthShuffle();
array_chronumb.knuthShuffle();
var audio = document.getElementById('sound');
audio.src = 'url-redacted' + array_chromatic[0] + '.mp3';
var str1 = array_chromatic[0]
str2 = str1.replace("%23", '#');
document.getElementById('knuth_data2').innerHTML = str2;// + array_accidental[0];
document.getElementById('knuth_data3').innerHTML = array_chronumb[0];
}