472,958 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 3649
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Sonoman | last post by:
Hi all: I am trying to write a simple program that simulates asking several persons their birth day and it counts how many persons are asked until two have the same birth day. The problem that I...
7
by: Ioannis Vranos | last post by:
I want to create some random numbers for encryption purposes, and i wonder if the following scheme makes it more hard to guess the underneath number generation pattern, than the plain use of...
5
by: user | last post by:
Hello i call it in almost same time (from two different threads): Random rand = new Random(); id=rand.Next(65535); and i often receive the same results. Is it possible ? How windows count this...
13
by: Roy Gourgi | last post by:
Hi, How do I invoke the random number generator that was suggested by a few people. Ideally, what I would like to do is to instantiate the random no. generator with a seed value that does not...
10
by: Curt_C [MVP] | last post by:
If I use it in my page it's fine but when I put it in a Class file for calling it returns the same # for each call. Any ideas why? I'm sure it's something I'll slap myself for but the only samples...
3
by: Barry | last post by:
I am looking for opinions on using random number generation in vb.net, I am using randomize and then using the rnd function, I have done a test and it seems to be pretty much random, what I'd like...
7
by: alphaLaura | last post by:
Hi all - I'm just new to the group and I hope nobody minds me asking for some help. I currently have an assignment which deals with matrices (more specifically, Gauss-Seidel solving of...
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
16
by: jason.cipriani | last post by:
I am looking for a random number generator implementation with the following requirements: - Thread-safe, re-entrant. - Produces consistently reproducible sequences of psuedo-random numbers...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.