random value generation

Bengt Richter bokr at oz.net
Thu Aug 14 17:51:52 CEST 2003

On Tue, 12 Aug 2003 11:57:28 -0700 (PDT), Brandon Michael Moore <brandon at 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
>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
>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)
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
 <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
 <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.

Bengt Richter

More information about the Python-list mailing list