random value generation

Brandon Michael Moore brandon at its.caltech.edu
Tue Aug 12 14:57:28 EDT 2003


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







More information about the Python-list mailing list