Entry
Noob asking: How do I get a program execute a random function?
Aug 2nd, 2007 07:41
stephen brown, Jimmy Hill,
===========================
from random import randint
def fun1():
print "Function 1 invoked"
return
def fun2():
print "Function 2 invoked"
return
def fun3():
print "Function 3 invoked"
return
def fun4():
print "Function 4 invoked"
return
funlist = [fun1, fun2, fun3, fun4]
funlist[randint(0, len(funlist)-1)]()
===========================
I'm not sure why you'ld want to do this. In general, it will be better
to just have one function whose behaviour is random, e.g.
===========================
from random import randint
def ranfun():
print "Function", randint(1,4), "invoked"
return
ranfun()
===========================
which has the same behaviour and is easier to read.