0

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

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)
  • Aside from the issue of repeating the random number stream, `randomValues` only generates one row of values - so if you want to get all five values again, you'll have to call it 5 times, just like you did to get them the first time. – Karl Knechtel Feb 03 '22 at 23:42
  • Unless you actually did want to **store** the data in a variable, in which case a) the question doesn't actually have anything to do with random numbers and b) there is no reason to call the function again - just use the stored data directly. – Karl Knechtel Feb 03 '22 at 23:43

2 Answers2

1

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))
Woodford
  • 3,746
  • 1
  • 15
  • 29
0

I suggest that you save the values in a variable, probably a list. Then you can reuse those values whenever you want.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268