how to do random / SystemRandom switch

Discussion in 'Python' started by Matthias Kievernagel, Apr 30, 2011.

  1. Dear list,

    In my top-level script I want to select if my program
    is to use random.xxx functions or the random.SystemRandom.xxx
    ones. All the other modules shouldn't know about that
    switch and simply use
    import random
    ...
    return random.randint(1, 6)
    ...
    for example.

    In C I would do a similar thing in the Makefile using
    Compiler-/Link-Options (DEBUG/FINAL Build) switching
    between two libraries.

    Any hints are welcome, especially a search term
    would be very helpful :)

    Thanks in advance,
    Matthias Kievernagel.
     
    Matthias Kievernagel, Apr 30, 2011
    #1
    1. Advertising

  2. Am 30.04.2011 11:10 schrieb Matthias Kievernagel:

    > In my top-level script I want to select if my program
    > is to use random.xxx functions or the random.SystemRandom.xxx
    > ones.


    On which criteria do you fix that?


    Anyway, you could use a module myrandom.py:

    import random

    if use_sys:
    randobj = random.SystemRandom()
    else:
    randobj = random.Random()

    and then use

    from myrandom import randobj as random

    and then use random's methods (randrange, random, whatever), no matter
    where they come from.


    Thomas
     
    Thomas Rachel, Apr 30, 2011
    #2
    1. Advertising

  3. Matthias Kievernagel

    Peter Otten Guest

    Matthias Kievernagel wrote:

    > In my top-level script I want to select if my program
    > is to use random.xxx functions or the random.SystemRandom.xxx
    > ones. All the other modules shouldn't know about that
    > switch and simply use
    > import random
    > ...
    > return random.randint(1, 6)
    > ...
    > for example.


    You can inject the SystemRandom instance into the sys.modules cache:

    >>> import random
    >>> random

    <module 'random' from '/usr/lib/python2.6/random.pyc'>
    >>> sr = random.SystemRandom()
    >>> import sys
    >>> sys.modules["random"] = sr


    Then use it in the other modules:

    >>> import random
    >>> random

    <random.SystemRandom object at 0x1acdb60>

    Another approach is to monkey-patch the random module:

    import random
    sr = random.SystemRandom()
    random.randrange = sr.randrange
    random.randint = sr.randint
    ....
     
    Peter Otten, Apr 30, 2011
    #3
  4. Peter Otten <> wrote:
    > Matthias Kievernagel wrote:
    >
    >> In my top-level script I want to select if my program
    >> is to use random.xxx functions or the random.SystemRandom.xxx
    >> ones. All the other modules shouldn't know about that
    >> switch and simply use
    >> import random
    >> ...
    >> return random.randint(1, 6)
    >> ...
    >> for example.

    >
    > You can inject the SystemRandom instance into the sys.modules cache:
    >
    >>>> import random
    >>>> random

    > <module 'random' from '/usr/lib/python2.6/random.pyc'>
    >>>> sr = random.SystemRandom()
    >>>> import sys
    >>>> sys.modules["random"] = sr

    >
    > Then use it in the other modules:
    >
    >>>> import random
    >>>> random

    > <random.SystemRandom object at 0x1acdb60>
    >
    > Another approach is to monkey-patch the random module:
    >
    > import random
    > sr = random.SystemRandom()
    > random.randrange = sr.randrange
    > random.randint = sr.randint
    > ...
    >

    Thanks a lot. That's what I was looking for.
    I'll give both a try.

    Regards,
    Matthias Kievernagel
     
    Matthias Kievernagel, Apr 30, 2011
    #4
    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. Darren Clark

    Random NOt random?

    Darren Clark, Jun 24, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    481
    mikeb
    Jun 24, 2004
  2. keytostars
    Replies:
    4
    Views:
    518
    keytostars
    Mar 15, 2007
  3. globalrev
    Replies:
    4
    Views:
    797
    Gabriel Genellina
    Apr 20, 2008
  4. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    123
    Lasse Reichstein Nielsen
    Apr 22, 2006
  5. VK
    Replies:
    15
    Views:
    1,284
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page