How to run functions in random order?
<p>I want to run functions with random order. It looks like "shuffle" function which shuffle a variables list.</p>

<p>Input:</p>

<pre><code>def a():
    print('a')

def b():
    print('b')

def c():
    print('c')

shuffle([a,b,c])
</code></pre>

<p>This is output what I want:</p>

<pre><code>a
b
c
</code></pre>

<p>or</p>

<pre><code>a
c
b
</code></pre>

<p>or </p>

<pre><code>c
b
a
</code></pre>

<p>or etc
How to run functions with random order?</p>

<p><a href="https://docs.python.org/3/library/random.html#random.shuffle" rel="nofollow"><code>random.shuffle</code></a> is an in-place operation. So you need to keep the list separately and shuffle it.</p>

<pre><code>functions = [a, b, c]
shuffle(functions)
</code></pre>

<p>Now, the functions are shuffled and you just have to execute them</p>

<pre><code>for func in functions:
    func()
</code></pre>

<hr>

<p>You can probably store this in a function and do it like this</p>

<pre><code>def run_functions_in_random_order(*funcs):
    functions = list(funcs)
    shuffle(functions)
    for func in functions:
        func()

run_functions_in_random_order(a, b, c)
</code></pre>

<hr>

<p>Or you can simply use the functions in closures, like this</p>

<pre><code>def run_functions_in_random_order(*funcs):
    def run():
        functions = list(funcs)
        shuffle(functions)
        for func in functions:
            func()
    return run

random_exec = run_functions_in_random_order(a, b, c)

random_exec()
random_exec()
random_exec()
</code></pre>

python function python-3.x random