Skip to content Skip to sidebar Skip to footer

How To Call A Random Function One Time In Javascript?

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'

Solution 1:

Check this.If you want to execute only one time , call execute(); only one time. If you want more time, call accordingly.

functionfunc1() {
   alert("1");
}

functionfunc2() {
   alert("2");
}

functionfunc3() {
    alert("3");
}
functionrandom(){
  var i  = Math.floor(Math.random()*20)%4;
  if(i<=0) returnrandom();
  return i;
}
functionexecute(){
  var i = random();
  eval('func'+i+'()');
}
execute();
execute();
execute();

Solution 2:

First make an array:

var myArray = ["func1", "func2", "func3"]

Then shuffle the array:

functionshuffle(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;
  }

  returnarray;
}
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.

Solution 3:

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.

Solution 4:

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;

functionstart() { // 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
 }
}

Solution 5:

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.

functionfunc1() {
    alert("1");
}

functionfunc2() {
    alert("2");
}

functionfunc3() {
    alert("3");
}

var funcs = [
    func1,
    func2,
    func3
]

functionexecute() {
    var i = Math.floor(Math.random() * funcs.length)
    funcs.splice(i-1, 1)[0]()
}


execute()
execute()
execute()

Post a Comment for "How To Call A Random Function One Time In Javascript?"