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

Making the random.randint(a,b) change everytime when using a while loop?

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):

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

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.

>Solution :

The problem is, that your dictonary gets populated with random values only ones. The method random.randint(0, 100) will return a random value ones and not every time you make use of your dictonary, 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 egain.

To solve the problem you could create a function to re-initialize your dictonary each time you make use of it, like I did down below.

def initialize_dictonary():
    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_dictonary() # 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 dictonary, 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 dictonary, 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)
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