The problem you are facing is that random.randint(1,UPPER_BOUND) is evaluated once during the call to the inner() function returned by runner() . You need to postpone the assessment until the end.
You can try something like this:
>>> def runner(f, callable): ... def inner(): ... for i in xrange(1000): ... f(*callable()) ... return inner ... >>> runner(f, lambda: (random.randint(1, 1000),))() 603 385 321 etc.
Note that callable is called every time the original function f called. Also note that callable must return a sequence type, such as a tuple or list.
Edit: if you need to pass other arguments to f , you can do it something like this:
>>> def runner(f, callable): ... def inner(*args, **kwds): ... for i in xrange(1000): ... pos = list(callable()) ... pos.extend(args) ... f(*pos, **kwds) ... return inner ... >>> def f(a, b, c, d = 3): ... print a, b, c, d ... >>> runner(f, lambda: (random.randint(1,1000),))(3, 5, d = 7) 771 3 5 7 907 3 5 7 265 3 5 7
source share