mercredi 12 mai 2021

How to get random values using a dictionary to associate the ranges

I'm stuck on an issue with random number ranges.

I have 4 values and a dictionary with a range of random numbers:

import random

val1 = 1
val2 = 1
val3 = 2
val4 = 3

dict = {1: random.randrange(1, 10),
        2: random.randrange(10, 20),
        3: random.randrange(20, 30)}

for key in dict:
    if key == val1:
        out1 = dict[key]
    if key == val2:
        out2 = dict[key]

print(out1, out2)

# Output
  5 5

I thought this would've been a clean approach but my issue comes if any of the val variables have the same value, the outputs are the same. The random value gets associated with the key and doesn't change. Is there a better approach so that each val will have its own random number?

I appreciate any advice, Thank you!




Aucun commentaire:

Enregistrer un commentaire