2016-03-03 89 views
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 
}; 


請我需要一個明確的答案,如果可能的。 謝謝

+0

這是JavaScript腳本嗎?無論如何,請標記語言。 –

+0

這是Javascript,請原諒我,我沒有提到它。 – catapultedplastic

+0

任何人請.. ..? – catapultedplastic

回答

1

這應該很容易。要做到這一點,你需要記住你以前使用過的功能。你的算法應該這樣工作:

  1. 創建一個數組[0,1,2,3]
  2. Shuffle it
  3. 根據混洗訂單運行您的功能。
  4. 當您的洗牌順序爲空時,從步驟1重複。