English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

假如我有一組陣列是
Array=[A,A,B,B,C,C,D,D,E,E,F,F,G,G,H,H];
現在我需要將此陣列順序用亂數語法從新排列
例如變成:
Array=[A,D,E,F,G,D,F,A,B,B,H,G,E,C,C,H];
請問我要如何寫(或是丟到另一組陣列也可以)??

2007-09-04 14:28:00 · 1 個解答 · 發問者 jones 1 in 電腦與網際網路 程式設計

1 個解答

大概如此這般:

/*取範圍亂數函式*/
function randomRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random()*(max-min+1)) + min;
return randomNum;
}

var myArray:Array = Array["A","A","B","B","C","C","D","D","E","E","F","F","G","G","H","H"];

var tmp="" , n:Number=0;
for (var i=0; i
/*取陣列內亂數*/
n=randomRange(0, myArray.length-1);

/*互換*/
tmp=Array[i];
Array[i]=Array[n];
Array[n]=tmp;
}

2007-09-04 18:11:51 · answer #1 · answered by mh 7 · 0 0

fedest.com, questions and answers