random value generation

  • Thread starter Brandon Michael Moore
  • Start date
B

Brandon Michael Moore

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
 
B

Bengt Richter

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.,
'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.'
'030814 120000'

Set some seed to have a way to restart the same sequence if desired
Vary times uniformly forawrd 24 hours from midday 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:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top