Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to do random / SystemRandom switch

Reply
Thread Tools

how to do random / SystemRandom switch

 
 
Matthias Kievernagel
Guest
Posts: n/a
 
      04-30-2011
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.


 
Reply With Quote
 
 
 
 
Thomas Rachel
Guest
Posts: n/a
 
      04-30-2011
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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      04-30-2011
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
....

 
Reply With Quote
 
Matthias Kievernagel
Guest
Posts: n/a
 
      04-30-2011
Peter Otten <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Random not really random... Maziar Aflatoun ASP .Net 4 08-05-2004 01:26 AM
Random NOt random? Darren Clark ASP .Net 3 06-24-2004 05:23 PM



Advertisments