admin管理员组文章数量:1315241
I have 3 functions, I want to be able to run them each randomly without using one more than twice. Here's some code I quickly put together:
function func1() {
alert("1");
}
function func2() {
alert("2");
}
function func3() {
alert("3");
}
I have 3 functions, I want to be able to run them each randomly without using one more than twice. Here's some code I quickly put together:
function func1() {
alert("1");
}
function func2() {
alert("2");
}
function func3() {
alert("3");
}
Share
Improve this question
edited Feb 24, 2017 at 4:01
user663031
asked Feb 24, 2017 at 3:46
user6124043user6124043
611 silver badge6 bronze badges
5
- 1 What have you tried? The code you provided doesn't even attempt to call any function. If you don't have time to try why should I take time to do it for you? – dmeglio Commented Feb 24, 2017 at 3:50
- 2 If you put all your functions into an array, it will bee a "how to take a random item from an array" question. I believe that there is already an answer to it at StackOverflow. – Yeldar Kurmangaliyev Commented Feb 24, 2017 at 3:54
- I have tried using the Math.random to generate a random number, and then use an if to find if the number matches a variable. The problem is I have to reuse this over and over, and will have to manually remove numbers from the list of numbers it can pull. – user6124043 Commented Feb 24, 2017 at 3:54
-
2
funcs = [func1, func2, func3]; funcs[Math.floor(Math.random() * funcs.length)]();
. – user663031 Commented Feb 24, 2017 at 4:00 - I have a solution, I am just on mobile... I can submit it soon. – Assafi Cohen-Arazi Commented Feb 24, 2017 at 4:11
5 Answers
Reset to default 2Check this.
If you want to execute only one time , call execute();
only one time. If you want more time, call accordingly.
function func1() {
alert("1");
}
function func2() {
alert("2");
}
function func3() {
alert("3");
}
function random(){
var i = Math.floor(Math.random()*20)%4;
if(i<=0) return random();
return i;
}
function execute(){
var i = random();
eval('func'+i+'()');
}
execute();
execute();
execute();
First make an array:
var myArray = ["func1", "func2", "func3"]
Then shuffle the array:
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;
}
shuffle(myArray)
Lastly, use this for loop:
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] == "func1") {
func1();
}
else if (myArray[i] == "func2") {
func2();
}
else if (myArray[i] == "func3") {
func3();
}
}
Hope this helped :)
*I know there is a accepted response, I am just introducing an alternative.
You probably want to run the following algo:
You have N functions - Run random to pick one out of the N.
Remove it from the array(you dont have to actual remove it, you could do the C++ trick of vector's "remove", i.e. move it to the back and update an iterator like element). Run Random to pick one out of the N - 1.
Continue to iterate until done with the array. Simple, O(N) solution.
On my Chrome, Linux Ubuntu 16.10 , Version 54.0.2840.71 (64-bit), the accepted solution does not work correctly. It does not run every single function exactly once. e.g. it will run 1, 1, 3.
Similar to Assafi's Answer, You can use Math.random() with an array to do this easily.
Best of yet (unlike the Accepted Answer) NO eval() !!!
First make an Array, (If you add Duplicate Values, You can technically make a "Percentage" or "Higher/Lower Chance" that a function will be executed over another.
// F1 and F2 have equal chances of Activating, while F3 is Rare.
// Remove Duplicates for them ALL to have an Equal Chance.
var array = ['F1','F1','F2','F2','F3']
Next, you want to use a function with If Statements, along with an Array Randomizer.
var ranFunc;
function start() { // Randomly Execute Function
ranFunc = array[Math.floor(Math.random() * array.length)];
if (ranFunc == 'F1') {
// do stuff
}
if (ranFunc == 'F2') {
// do stuff
}
if (ranFunc == 'f3') {
// do stuff
}
}
Simply organize your functions in an array, use random generator, and then splice array based on random number.
This way, you will only call each function once.
function func1() {
alert("1");
}
function func2() {
alert("2");
}
function func3() {
alert("3");
}
var funcs = [
func1,
func2,
func3
]
function execute() {
var i = Math.floor(Math.random() * funcs.length)
funcs.splice(i-1, 1)[0]()
}
execute()
execute()
execute()
本文标签: How to call a random function one time in JavascriptStack Overflow
版权声明:本文标题:How to call a random function one time in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741980783a2408383.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论