how to do random / SystemRandom switch

  • Thread starter Matthias Kievernagel
  • Start date
M

Matthias Kievernagel

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.
 
T

Thomas Rachel

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
 
P

Peter Otten

Matthias said:
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
sr = random.SystemRandom()
import sys
sys.modules["random"] = sr

Then use it in the other modules:
<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
....
 
M

Matthias Kievernagel

Peter Otten said:
Matthias said:
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
sr = random.SystemRandom()
import sys
sys.modules["random"] = sr

Then use it in the other modules:
<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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top