473,769 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1475
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.LVbK4P 6cM3@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(b ar)()

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(f unctions)(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(r andom.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.r andom()))]() # 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*******@gmai l.comwrote:
if you have a list of functions you can try this:

import random
import math
m[int(math.floor( len(m)*random.r andom()))]() # 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...@gmai l.comwrote:
if you have a list of functions you can try this:
import random
import math
m[int(math.floor( len(m)*random.r andom()))]() # 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
2982
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
5029
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 same color, but after every refresh a new random one. thanx 4 your support
8
3040
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 what I really should do. I've had a look through Programming Python and the Python Cookbook, which have given me ideas, but nothing has gelled yet, so I thought I'd put the question to the community. But first, let me be a little more detailed...
4
8249
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 number for ALL the records. It appears the function is only getting called once, therefore only one random number is being generated for all the records. I want a different random number for each record. Is this possible, or do I have to do it all...
10
2678
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 textbox control. I need a good seed like ip address or something. 'Function to generate random numbers Public Function GetRandomNumber() As Integer
1
1402
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 <? require_once("system_fns.php"); session_start(); do_html_header("Changing password");
2
2105
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(); do_html_header("Changing password"); //check_valid_user(); $new_passwd=($_POST);
3
2085
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: ************************************************* Function RandomString(size as integer, lowerCase as boolean) as string Dim builder as StringBuilder = new StringBuilder() Dim random as Random = new Random() Dim i as integer dim ch as char
15
1893
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
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.