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 -- 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
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
How are you making the list of functions? Something like this: -
fs = []
-
for k,f in globals():
-
if callable(f):
-
fs.append(f)
-
Then to call a random one (assuming they all take no parameters): -
import random
-
random.choice(fs)()
-
Or maybe you only have the name of the function, this should work:
Does this help? I'm not really clear on what you are asking for.
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)()
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
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....
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...
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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;
}
|
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
|
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...
|
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...
|
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
| |
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");
|
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);
|
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
|
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
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |