2

I have the following problem:

I want to call my functions func1(), func2() & func3() in a random order. But i want to be sure that every function is called!

If it's possible it also would be nice that no functions are used; just a random order of code sequences. Like this:

function xy(){

   //Call this sequence first second or third
   doSomething1

   //Call this sequence first second or third
   doSomething2

   //Call this sequence first second or third
   doSomething3

   //!! But call each sequence !!

}


Thanks in advance ;)

2
  • @NinaScholz random order call? Commented Dec 5, 2015 at 22:34
  • it depends of the purpose. Commented Dec 5, 2015 at 22:37

3 Answers 3

3

You could put all function names as strings into an Array, then sort this array randomly and call the functions:

var funcArr = new Array("func1", "func2", "func3");
shuffle(funcArr); //You would need a shuffle function for that. Can be easily found on the internet.

for (var i = 0; i < funcArr.length; i++)
    window[funcArr[i]]();

EDIT: If you don't want functions but lines of code to be sorted randomly, then that's not going to work. JavaScript does not have a goto command (at least not without an external API), so you can't jump between code lines. You can only mix functions, as shown above.

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

7 Comments

Do you think there is possiblity to do this without external functions, just with code sequences? --> 3 sequences called in random order.
What do you mean by external functions? The shuffle method? And does that exclude functions like Math.random() as well? Because otherwise there is no possibility.
I have code seq1, code seq2, ... and want to call them in a random order
Yeah, but what do you mean by external functions? That idea requires some kind of randomness, and randomness can only be created by using functions such as Math.random().
I dont want to do function xy(){...}, i want to randomise the sequences in a function
|
2

You can use something like the Fisher-Yates shuffle for shuffling the functions and then call them via Array.prototype.forEach():

var a = function () { alert('a'); },
    b = function () { alert('b'); },
    c = function () { alert('c'); },
    array = [a, b, c];

array = array.map(function (a, i, o) {
    var j = (Math.random() * (o.length - i) | 0) + i,
        t = o[j];
    o[j] = a;
    return t;
});

array.forEach(function (a) { a(); });

Comments

1

There is many ways to do that, one of the most easy way is like this:

Array.prototype.shuffle = function () {
  this.sort(function() { return 0.5 - Math.random() });
}

[func1, func2, func3].shuffle().forEach(function(func, index, array){
  func();
});

Comments

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.