random value generation

Discussion in 'Python' started by Brandon Michael Moore, Aug 12, 2003.

  1. 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
    Brandon Michael Moore, Aug 12, 2003
    #1
    1. Advertising

  2. On Tue, 12 Aug 2003 11:57:28 -0700 (PDT), Brandon Michael Moore <> 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
    Bengt Richter, Aug 14, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ALuPin

    Random generation in function

    ALuPin, Sep 1, 2004, in forum: VHDL
    Replies:
    1
    Views:
    649
    Ralf Hildebrandt
    Sep 1, 2004
  2. dpi

    Random Number Generation

    dpi, Mar 13, 2006, in forum: VHDL
    Replies:
    4
    Views:
    10,044
    vipinlal
    Mar 26, 2010
  3. globalrev
    Replies:
    4
    Views:
    741
    Gabriel Genellina
    Apr 20, 2008
  4. John W. Long

    HTML Generation (Next Generation CGI)

    John W. Long, Nov 22, 2003, in forum: Ruby
    Replies:
    4
    Views:
    316
    John W. Long
    Nov 24, 2003
  5. VK
    Replies:
    15
    Views:
    1,115
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page