5

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");
    }
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? Commented Feb 24, 2017 at 3:50
  • 2
    If you put all your functions into an array, it will become a "how to take a random item from an array" question. I believe that there is already an answer to it at StackOverflow. 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. Commented Feb 24, 2017 at 3:54
  • 2
    funcs = [func1, func2, func3]; funcs[Math.floor(Math.random() * funcs.length)]();. Commented Feb 24, 2017 at 4:00
  • I have a solution, I am just on mobile... I can submit it soon. Commented Feb 24, 2017 at 4:11

5 Answers 5

2

Check 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();

Sign up to request clarification or add additional context in comments.

10 Comments

Alright, thank you. Just a couple of questions (I'm newer to javascript): Could you explain these two lines: eval('func'+i+'()'); var i = Math.floor(Math.random()*20)%4;
if it works for you, mark it as answer will help future visitors
This is not working for me... it repeats the same function twice sometimes, maybe even three times...
Run the code snippet 5+ times. There is a percentage error.
we can't predict the random value. In this example we are selecting 1,2,3 values. So probabilities are there. I will edit my answer please wait
|
1

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.

9 Comments

javascript is a clientside language and you are putting heavy load to client. It is very bad approach
@SagarV Better than the accepted answer... It uses eval() EVAL IS EVIL. But if its THAT BAD, check my answer, its similar to this but "lighter"
@TaylorSpark eval is not evil in JavaScript. Recommended reading : stackoverflow.com/a/197823/2427065
@SagarV The use of eval is bad convention and, when used, docks points off of linters such as PyLint3 and JSLint. eval isn't evil, it's just bad convention.
@SagarV as Assafi said, eval() is dangerous, it presents many security risks, Much more than most of the "dangerous" methods to use. if you place eval in a function without placing a permanent value, and something this too can be bypassed, You can actually give ANYONE full access to your internal JS of the site, and if it runs on PHP, it can be used to send dangerous functions and code into your website. If you want to actually use eval(), then switch to the Math.#() functions, and use window[](); for functions and variables via a string
|
1

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.

Comments

0

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
 }
}

Comments

0

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()

1 Comment

Solution doesn't require eval, which opens your code to attacks... stackoverflow.com/questions/86513/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.