468,541 Members | 1,809 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,541 developers. It's quick & easy.

random value generation

I'm trying to test a web application using a tool written in python. I
would like to be able to generate random values to put in fields. I would
like to be able to generate random dates (in a specified range), random
strings (specifying allowed characters and a distribution of lengths), or
choose randomly between several generators (for better control of the
distribution of values).

Is there any library for this sort of thing in Python? I've started a
library heavily based off the generators of Haskell's QuickCheck library,
that represents generators as objects with a generate() method, with
constructor parameters (maybe other generators) the control the resulting
distribution.

I've got Choose that takes a list of generators and chooses between them
(with random.choice), GMap that maps a function over the output of another
generator, Choice that chooses an element from a list, Expo that wraps
random's expovariate, and List that takes a number generator and an
element generator and generates lists with lengths drawn from the first
generator and elements drawn from the second, and String that's like List
but joins the resulting list with ''.

I can use these classes like Choice(['red','blue','green']), or
String(Expo(1.0/3),Choice(string.lower)).

Are there any existing libraries for this sort of thing? Or at least, can
anyone suggest a cleaner way of doing this? (I don't like creating classes
for things that should really be functions, but I need to call the random
number generator each time I use the generator, not once when I define it)

Brandon

Jul 18 '05 #1
1 3468
On Tue, 12 Aug 2003 11:57:28 -0700 (PDT), Brandon Michael Moore <br*****@its.caltech.edu> wrote:
I'm trying to test a web application using a tool written in python. I
would like to be able to generate random values to put in fields. I would
like to be able to generate random dates (in a specified range), random
strings (specifying allowed characters and a distribution of lengths), or
choose randomly between several generators (for better control of the
distribution of values).

Is there any library for this sort of thing in Python? I've started a
library heavily based off the generators of Haskell's QuickCheck library,
that represents generators as objects with a generate() method, with
constructor parameters (maybe other generators) the control the resulting
distribution.

I've got Choose that takes a list of generators and chooses between them
(with random.choice), GMap that maps a function over the output of another
generator, Choice that chooses an element from a list, Expo that wraps
random's expovariate, and List that takes a number generator and an
element generator and generates lists with lengths drawn from the first
generator and elements drawn from the second, and String that's like List
but joins the resulting list with ''.

I can use these classes like Choice(['red','blue','green']), or
String(Expo(1.0/3),Choice(string.lower)).

Are there any existing libraries for this sort of thing? Or at least, can
anyone suggest a cleaner way of doing this? (I don't like creating classes
for things that should really be functions, but I need to call the random
number generator each time I use the generator, not once when I define it)

Brandon

Have you looked at the time and random modules? E.g.,
import time, random
time.localtime() (2003, 8, 14, 8, 45, 13, 3, 226, 1) midday = time.mktime((2003,8,14,12,0,0,0,0,1))
time.ctime(midday) 'Thu Aug 14 12:00:00 2003' time.strftime <built-in function strftime> time.strftime.__doc__ 'strftime(format[, tuple]) -> string\n\nConvert a time tuple to a string according to a format s
pecification.\nSee the library reference manual for formatting codes. When the time tuple\nis no
t present, current time as returned by localtime() is used.'
time.strftime('%Y%m%d %H%M%S',time.localtime(midday)) '20030814 120000' time.strftime('%y%m%d %H%M%S',time.localtime(midday)) '030814 120000'

Set some seed to have a way to restart the same sequence if desired random.seed(1234)
Vary times uniformly forawrd 24 hours from midday for i in xrange(10): print time.strftime('<%Y-%m-%d %H%M%S>', time.localtime(midday+random.r andom()*3600*24))
...
<2003-08-15 111141>
<2003-08-14 223439>
<2003-08-14 121047>
<2003-08-15 095148>
<2003-08-15 103232>
<2003-08-15 015824>
<2003-08-15 040703>
<2003-08-14 140052>
<2003-08-15 062343>
<2003-08-14 174100>

or vary uniformly forward one hour from midday:
for i in xrange(10): print time.strftime('<%Y-%m-%d %H%M%S>', time.localtime(midday+random.r

andom()*3600))
...
<2003-08-14 120150>
<2003-08-14 124719>
<2003-08-14 122045>
<2003-08-14 123723>
<2003-08-14 123656>
<2003-08-14 120854>
<2003-08-14 121059>
<2003-08-14 120651>
<2003-08-14 120052>
<2003-08-14 122912>

of course you can use other distributions and time formats too. This was just a quick demo.

Generating and/or selecting random strings seems not too challenging, unless you
have some tricky requirements.

Regards,
Bengt Richter
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Sonoman | last post: by
7 posts views Thread by Ioannis Vranos | last post: by
5 posts views Thread by user | last post: by
13 posts views Thread by Roy Gourgi | last post: by
10 posts views Thread by Curt_C [MVP] | last post: by
3 posts views Thread by Barry | last post: by
7 posts views Thread by alphaLaura | last post: by
21 posts views Thread by chico_yallin | last post: by
16 posts views Thread by jason.cipriani | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.