函数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,1,2,3]
Shuffle it。
根据改组的顺序运行功能。
当您的混洗订单为空时,请从步骤1开始重复。
关于javascript - 无重复随机功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35773800/