472,958 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1436
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: JNY | last post by:
I am using random to generate random numbers, thus: int x,y; for (y = 0;y < 5;y++) { x = random(50); cout << x; }
3
by: TaTonka | last post by:
hi! how can i manage it (html or jscript with css) that everytime a user loads or refreshes a page, the page has a new bgcolor. i want to put it in a single file, so that all my pages have the...
8
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to...
4
by: Greg Strong | last post by:
Hello All, Is it possible to create multiple random numbers in a query where there are numerous records? I've created a custom function. When I use it in a query it creates the same random...
10
by: Leon | last post by:
I know by default the random number generator use the time, but what is the best seed I can used in my web application? The Program generate 6 unique random numbers and load each of them in a...
1
by: muchexie | last post by:
i have two scripts that are not running to reset a password that has been forgotten and the other to change old password. here are the scripts. change_passwd.php <? ...
2
by: muchexie | last post by:
i have two scripts that are not running to reset a password that has been forgotten and the other to change old password. here are the scripts. change_passwd.php session_start();...
3
by: tshad | last post by:
I have a page that I am getting a username and password as a random number (2 letters, one number and 4 more letters) I have 2 functions I call: *************************************************...
15
by: ianweise | last post by:
hello, before i post my code for this, is there anyone out there at this moment? no sense in posting if no one is out there to read and answer it =P
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.