In the code that I'm writing, I have a nested dictionary which contains a key that has a value of random.randint(0,100)
. Below this code is a while loop that contains functions which need the items from the dictionary.
When the while loop runs again, the random.randint(0,100)
outputs the same value. How do I make it so the random value is different? I tried making a new .py file and placing the random.randint(a,b)
under variables in a While true
loop, but the code in the main file never starts. I couldn't put the dictionary inside of the while loop or after it ended because then the code wouldn't work.
For reference, this is what the code looks like (making a summary of it because the original code is too long to paste):
A = {'Key1': {'Key A1': 1, 'Key B1': 5, 'Random': random.randint(0,100)},
'Key2': {'Key A2': 4, 'Key B2': 7, 'Random': random.randint(5,100)}
'Key3': {'Key A3': 5, 'Key B3': 13, 'Random': random.randint(5,90)}}
while I > 5:
def func(a,b):
Rand_Value = A['Key1']['Random']
print(f'The value for this is {Rand_Value}')
func(10,5)
I'm new to coding, my apologies if the question is not as good as it could be.
CodePudding user response:
The problem is, that your dictionary gets populated with random values only once. The method random.randint(0, 100)
will return a random value once and not every time you make use of your dictionary, eg.:
A = {'Key1': {'Key A1': 1, 'Key B1': 5, 'Random': random.randint(0,100)},
'Key2': {'Key A2': 4, 'Key B2': 7, 'Random': random.randint(5,100)}
'Key3': {'Key A3': 5, 'Key B3': 13, 'Random': random.randint(5,90)}}
might become
A = {'Key1': {'Key A1': 1, 'Key B1': 5, 'Random': 7},
'Key2': {'Key A2': 4, 'Key B2': 7, 'Random': 18}
'Key3': {'Key A3': 5, 'Key B3': 13, 'Random': 96}}
As you can see the random values are now fixed and won't change ever again.
To solve the problem you could create a function to re-initialize your dictionary each time you make use of it, like I did down below.
def initialize_dictionary():
return {
'Key1': {'Key A1': 1, 'Key B1': 5, 'Random': random.randint(0,100)},
'Key2': {'Key A2': 4, 'Key B2': 7, 'Random': random.randint(5,100)},
'Key3': {'Key A3': 5, 'Key B3': 13, 'Random': random.randint(5,90)}
}
# do stuff
while i < 5:
def func(a, b):
my_new_dict = initialize_dictionary() # will create a new dict with new random values
Rand_Value = my_new_dict['Key1']['Random']
print(f'The value for this is {Rand_Value}')
func(10, 5)
Nevertheless I would recommend you to use something like the following, because the above solution can result into very slow performance speed, if you have a huge dictionary, which needs to be re-initialized over and over again.
# do stuff
while i < 5:
def func(a, b):
Random_Value = random.randint(0, 100)
print(f'The value for this is {Rand_Value}')
func(10, 5)
Tip: You could add two key-value pairs in your dictionary, which will give you the starting and end value of your range, if you need different ranges for each key.
A = {'Key1': {'Key A1': 1, 'Key B1': 5, 'start': 0, 'end': 100},
'Key2': {'Key A2': 4, 'Key B2': 7, 'start': 5, 'end': 100}
'Key3': {'Key A3': 5, 'Key B3': 13, 'start': 5, 'end': 90}}
start = A['Key1']['start']
end = A['Key1']['end']
Random_Value = random.randint(start, end)
CodePudding user response:
if putting random function in the dictionary is necessarily you can do it with lambda like this (do not forget to call it):
from random import randint
A = {'Key1': {'Key A1': 1, 'Key B1': 5, 'Random': lambda : randint(0,100)},
'Key2': {'Key A2': 4, 'Key B2': 7, 'Random': lambda : randint(5,100)},
'Key3': {'Key A3': 5, 'Key B3': 13, 'Random': lambda : randint(5,90)}}
res = [A['Key1']['Random']() for _ in range(5)]
#-------------------------^^ calling lambda
print(res)
'''
[59, 66, 42, 79, 100]
but the better solution would be replacing random function with it parameters in the dictionary (do not forget to unpack):
from random import randint
A = {'Key1': {'Key A1': 1, 'Key B1': 5, 'Random': (0,100)},
'Key2': {'Key A2': 4, 'Key B2': 7, 'Random': (5,100)},
'Key3': {'Key A3': 5, 'Key B3': 13, 'Random': (5,90)}}
res = [randint(*A['Key1']['Random']) for _ in range(5)]
#--------------^ unpacking
print(res)
'''
[8, 65, 10, 67, 76]