Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Retain original randomized number output for multiple function calls

I’m struggling with a particular issue in regards to having randomValues run as many times as in the range of TOTAL, but retain that information and not rerun again. Ideally have multiple functions call randomValues throughout but it is using the same 5 rows of randomValues from the first call. In the example below, no matter where I call randomValues() for the first time, it will remember those 3 values in 5 rows, so they can be called later on. Does that mean I store them somewhere for future use in the script?

import random
TOTAL = 5
def randomValues():
    a = random.randint(0,256)
    b = random.randint(0,256)
    c = random.randint(0,256)        
return a,b,c

for i in range(TOTAL): 
    # run this function for the first time, now remember those 3 values in 5 rows, so it can be called later on
    print ("valuesof35x" + str(i),randomValues())

# called later, and expect output to be the same as before
print ("valuesof35xagain",randomValues())
print ("valuesof35xagainagain",randomValues())

CURRENT OUTPUT

valuesof35x0 (55, 236, 30)
valuesof35x1 (59, 215, 108)
valuesof35x2 (207, 117, 210)
valuesof35x3 (127, 176, 112)
valuesof35x4 (185, 243, 218)
valuesof35xagain (84, 22, 196)
valuesof35xagainagain (251, 137, 208)

EXPECTED OUTPUT

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

valuesof35x0 (55, 236, 30)
valuesof35x1 (59, 215, 108)
valuesof35x2 (207, 117, 210)
valuesof35x3 (127, 176, 112)
valuesof35x4 (185, 243, 218)
valuesof35xagain (55, 236, 30)
valuesof35xagain (59, 215, 108)
valuesof35xagain (207, 117, 210)
valuesof35xagain (127, 176, 112)
valuesof35xagain (185, 243, 218)
valuesof35xagainagain (55, 236, 30)
valuesof35xagainagain (59, 215, 108)
valuesof35xagainagain (207, 117, 210)
valuesof35xagainagain (127, 176, 112)
valuesof35xagainagain (185, 243, 218)

>Solution :

You’ll need to save your random seed to return the PRNG back to a known state before generating your random numbers.

import random
SEED = random.random()
TOTAL = 5

def randomValues(prng):
    a = prng.randint(0,256)
    b = prng.randint(0,256)
    c = prng.randint(0,256)        
    return a,b,c

prng = random.Random()  # isolate your seed shenanigans to your own PRNG
prng.seed(SEED)
for i in range(TOTAL): 
    print("valuesof35x" + str(i), randomValues(prng))

prng.seed(SEED)
print("valuesof35xagain", randomValues(prng))

prng.seed(SEED)
print("valuesof35xagainagain", randomValues(prng))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading