Re: running a random function
On 8, 00:07, Dustan [EMAIL PROTECTED] wrote: On Jun 7, 1:30 pm, Neil Cerutti [EMAIL PROTECTED] wrote: On 2007-06-07, Stebanoid [EMAIL PROTECTED] wrote: if you have a list of functions you can try this: import random import math m[int(math.floor(len(m)*random.random()))]() # seems like Lisp Or rather m[random.randint(0, len(m))]() Or rather random.choice(m)() # seems like Python -- Neil Cerutti Caution: Cape does not enable user to fly. --Kenner's Batman costume Sorry :( I have no experience using this module in Python, and forget this functions because I newer use this. Sorry for my terrible English. :P -- http://mail.python.org/mailman/listinfo/python-list
Re: running a random function
On 2007-06-08, Stebanoid [EMAIL PROTECTED] wrote: On 8, 00:07, Dustan [EMAIL PROTECTED] wrote: On Jun 7, 1:30 pm, Neil Cerutti [EMAIL PROTECTED] wrote: On 2007-06-07, Stebanoid [EMAIL PROTECTED] wrote: if you have a list of functions you can try this: import random import math m[int(math.floor(len(m)*random.random()))]() # seems like Lisp Or rather m[random.randint(0, len(m))]() Or rather random.choice(m)() # seems like Python -- Neil Cerutti Caution: Cape does not enable user to fly. --Kenner's Batman costume Sorry :( I have no experience using this module in Python, and forget this functions because I newer use this. Sorry for my terrible English. :P No need to apologise. My solution above wasn't the best one posted either! -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list
running a random function
I would like to write some code that would randomly select a function from a list of functions and call it. I was looking in the globals names space and randomly selecting items that were of type function.. but I didn't see a way of actually running the function. Any pointers? -- David Bear -- let me buy your intellectual property, I want to own your thoughts -- -- http://mail.python.org/mailman/listinfo/python-list
Re: running a random function
In [EMAIL PROTECTED], David Bear wrote: I would like to write some code that would randomly select a function from a list of functions and call it. I was looking in the globals names space and randomly selecting items that were of type function.. but I didn't see a way of actually running the function. If you have a function or callable you call it with the call operator, which are parenthesis: In [8]: int Out[8]: type 'int' In [9]: int() Out[9]: 0 For a random selection of an element from a list look at the `random.choice()` function. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: running a random function
How are you making the list of functions? Something like this: [code] fs = [] for k,f in globals(): if callable(f): fs.append(f) [/code] Then to call a random one (assuming they all take no parameters): [code] import random random.choice(fs)() [/code] Or maybe you only have the name of the function, this should work: [code] globals()[name]() [/code] Does this help? I'm not really clear on what you are asking for. -- http://mail.python.org/mailman/listinfo/python-list
Re: running a random function
David Bear said unto the world upon 06/07/2007 11:56 AM: I would like to write some code that would randomly select a function from a list of functions and call it. I was looking in the globals names space and randomly selecting items that were of type function.. but I didn't see a way of actually running the function. Any pointers? Unless I've missed your meaning: import random def f(): print 42 ... def g(): print 24 ... funcs = [f,g] random.choice(funcs)() 24 Best, Brian vdB -- http://mail.python.org/mailman/listinfo/python-list
Re: running a random function
On Jun 7, 10:56 am, David Bear [EMAIL PROTECTED] wrote: I would like to write some code that would randomly select a function from a list of functions and call it. I was looking in the globals names space and randomly selecting items that were of type function.. Careful!!! You don't want to destroy your computer by accident. but I didn't see a way of actually running the function. What do you mean? foo is a function; here's how you run it: foo() Any pointers? Given a list of functions, it would simply be, given the list of functions bar (untested): import random random.choice(bar)() -- http://mail.python.org/mailman/listinfo/python-list
Re: running a random function
On 7 jun, 11:56, David Bear [EMAIL PROTECTED] wrote: I would like to write some code that would randomly select a function from a list of functions and call it. I was looking in the globals names space and randomly selecting items that were of type function.. but I didn't see a way of actually running the function. Try this: def f(x): print Calling f with arg %s % x def g(x): print Calling g with arg %s % x def h(x): print Calling h with arg %s % x import random functions = [f, g, h] for i in range(10): random.choice(functions)(25) HTH, cheers. -- Roberto Bonvallet -- http://mail.python.org/mailman/listinfo/python-list
Re: running a random function
On 7, 19:56, David Bear [EMAIL PROTECTED] wrote: I would like to write some code that would randomly select a function from a list of functions and call it. I was looking in the globals names space and randomly selecting items that were of type function.. but I didn't see a way of actually running the function. Any pointers? -- David Bear -- let me buy your intellectual property, I want to own your thoughts -- if you have a list of functions you can try this: import random import math m[int(math.ceil(random.random()))]() #seems like Lisp code :) but what about functions arguments? Strange task... Very strange -- http://mail.python.org/mailman/listinfo/python-list
Re: running a random function
On 7, 19:56, David Bear [EMAIL PROTECTED] wrote: I would like to write some code that would randomly select a function from a list of functions and call it. I was looking in the globals names space and randomly selecting items that were of type function.. but I didn't see a way of actually running the function. Any pointers? -- David Bear -- let me buy your intellectual property, I want to own your thoughts -- if you have a list of functions you can try this: import random import math m[int(math.floor(len(m)*random.random()))]() # seems like Lisp code :D where m - is list of functions. but what about functions arguments? Strange task.. Very strange... -- http://mail.python.org/mailman/listinfo/python-list
Re: running a random function
On 2007-06-07, Stebanoid [EMAIL PROTECTED] wrote: if you have a list of functions you can try this: import random import math m[int(math.floor(len(m)*random.random()))]() # seems like Lisp Or rather m[random.randint(0, len(m))]() -- Neil Cerutti Caution: Cape does not enable user to fly. --Kenner's Batman costume -- http://mail.python.org/mailman/listinfo/python-list
Re: running a random function
On Jun 7, 1:30 pm, Neil Cerutti [EMAIL PROTECTED] wrote: On 2007-06-07, Stebanoid [EMAIL PROTECTED] wrote: if you have a list of functions you can try this: import random import math m[int(math.floor(len(m)*random.random()))]() # seems like Lisp Or rather m[random.randint(0, len(m))]() Or rather random.choice(m)() # seems like Python -- Neil Cerutti Caution: Cape does not enable user to fly. --Kenner's Batman costume -- http://mail.python.org/mailman/listinfo/python-list
Re: running a random function
On 2007-06-07, Dustan [EMAIL PROTECTED] wrote: On Jun 7, 1:30 pm, Neil Cerutti [EMAIL PROTECTED] wrote: On 2007-06-07, Stebanoid [EMAIL PROTECTED] wrote: if you have a list of functions you can try this: import random import math m[int(math.floor(len(m)*random.random()))]() # seems like Lisp Or rather m[random.randint(0, len(m))]() Or rather random.choice(m)() # seems like Python Sweet! -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list