0
函數rnd_act_select()在代碼中進一步隨機化了四個函數,它被Photoshop中的一個按鈕調用了我已經設置好了,一切正常,但我不知道如何設置隨機在全部執行完畢之前不重複案例。有人能爲我解決這個問題嗎? 謝謝。沒有重複的隨機函數
function rnd_act_select() {
// random selection
NumberOfFunctions = 4;//Number of functions in your switch statement.
var ran = Math.floor(Math.random() * NumberOfFunctions);
switch(ran) {
case 0: func_one(); break;
case 1: func_two(); break;
case 2: func_three(); break;
case 3: func_four(); break;
default : return;
}
};
我已經試過這種方式,但它不工作:
function rnd_act_select() {
// random selection begin
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
var arr = [func_one,func_two,func_three,func_four];
var shuf = shuffle(arr);
switch(shuf) {
case 0: func_one(); break;
case 1: func_two(); break;
case 2: func_three(); break;
case 3: func_four(); break;
default : return;
}
// random selection end
};
請我需要一個明確的答案,如果可能的。 謝謝
這是JavaScript腳本嗎?無論如何,請標記語言。 –
這是Javascript,請原諒我,我沒有提到它。 – catapultedplastic
任何人請.. ..? – catapultedplastic