471,305 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

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 --
Jun 7 '07 #1
12 1371
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
Jun 7 '07 #2
In <1654405.LVbK4P6cM3@teancum>, 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
Jun 7 '07 #3
How are you making the list of functions? Something like this:

Expand|Select|Wrap|Line Numbers
  1. fs = []
  2. for k,f in globals():
  3. if callable(f):
  4. fs.append(f)
  5.  
Then to call a random one (assuming they all take no parameters):

Expand|Select|Wrap|Line Numbers
  1. import random
  2. random.choice(fs)()
  3.  
Or maybe you only have the name of the function, this should work:

Expand|Select|Wrap|Line Numbers
  1. globals()[name]()
  2.  
Does this help? I'm not really clear on what you are asking for.

Jun 7 '07 #4
On Jun 7, 10:56 am, David Bear <david.b...@asu.eduwrote:
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)()

Jun 7 '07 #5
On 7 jun, 11:56, David Bear <david.b...@asu.eduwrote:
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

Jun 7 '07 #6
On 7 , 19:56, David Bear <david.b...@asu.eduwrote:
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....

Jun 7 '07 #7
On 7 , 19:56, David Bear <david.b...@asu.eduwrote:
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...

Jun 7 '07 #8
On 2007-06-07, Stebanoid <St*******@gmail.comwrote:
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
Jun 7 '07 #9
On Jun 7, 1:30 pm, Neil Cerutti <horp...@yahoo.comwrote:
On 2007-06-07, Stebanoid <Steban...@gmail.comwrote:
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

Jun 7 '07 #10
On 2007-06-07, Dustan <Du**********@gmail.comwrote:
On Jun 7, 1:30 pm, Neil Cerutti <horp...@yahoo.comwrote:
>On 2007-06-07, Stebanoid <Steban...@gmail.comwrote:
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
Jun 7 '07 #11
On 8 , 00:07, Dustan <DustanGro...@gmail.comwrote:
On Jun 7, 1:30 pm, Neil Cerutti <horp...@yahoo.comwrote:
On 2007-06-07, Stebanoid <Steban...@gmail.comwrote:
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

Jun 8 '07 #12
On 2007-06-08, Stebanoid <St*******@gmail.comwrote:
On 8 , 00:07, Dustan <DustanGro...@gmail.comwrote:
>On Jun 7, 1:30 pm, Neil Cerutti <horp...@yahoo.comwrote:
On 2007-06-07, Stebanoid <Steban...@gmail.comwrote:
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
Jun 8 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

25 posts views Thread by JNY | last post: by
3 posts views Thread by TaTonka | last post: by
8 posts views Thread by Paul Cochrane | last post: by
4 posts views Thread by Greg Strong | last post: by
10 posts views Thread by Leon | last post: by
1 post views Thread by muchexie | last post: by
3 posts views Thread by tshad | last post: by
15 posts views Thread by ianweise | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.