#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    35
    Rep Power
    12

    Question about 'random'


    If I have multiple files in a single project using function randint, should I run seed() somewhere in each file? Or can I just run seed() once and have it seed all randint calls across all files?

    This question may seem silly, but I remember in C++ that the more you call seed, the less random your numbers are! If this is the case in Python, I'd like to avoid that effect...

    Thanks,
    tps
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I've used 'random' in alot of different applications and i have to say i've never once needed seed() , all you really need is randint(), then let Python handle the rest

    Code:
    >>> import random
    >>> random.randint(1, 10)
    5
    >>> random.randint(1, 10)
    6
    >>> random.randint(1, 10)
    2
    >>> random.randint(1, 10)
    1
    >>> random.randint(1, 10)
    7
    >>> random.randint(1, 10)
    2
    >>> random.randint(1, 10)
    7
    >>> random.randint(1, 10)
    2
    >>> random.randint(1, 10)
    8
    >>>
    Let me know if you have any questions

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    seed() is for making reproducible results, really. Python uses the "Mersenne Twister" algorithm to produce random numbers, and while it is periodic, it has a HUGE period. In fact, I know the exact size: (2**19937)-1. So if you start at the same point, you actually will get the same set of values for each time after. Observe:
    Code:
    >>> import random
    >>> random.randint(1,100)
    36
    >>> random.randint(1,100)
    93
    >>> random.seed(42)
    >>> random.randint(1,100)
    64
    >>> random.randint(1,100)
    3
    >>> random.randint(1,100)
    28
    >>> random.seed(42)
    >>> random.randint(1,100)
    64
    >>> random.randint(1,100)
    3
    >>> random.randint(1,100)
    28
    It's a way you can test and make sure you are getting the "expected" random value in automated tests, for example. But you never have to explicitly seed the generator, as it will automatically seed it with the system time, I believe.
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    The random generation module automatically seeds itself with the current system time, when you first import the module. Also, another interesting thing to note is that if you call random.seed() in your code with no arguments (or None), it will use the current system time as the seed.
    [edit]Note that older versions of python (below 2.1) used Wichmann Hill instead of Mersenne Twister as the algorithm for the random module.[/edit]
    Last edited by Scorpions4ever; January 25th, 2004 at 12:37 AM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo