samedi 24 octobre 2020

Generate new random numbers each time variable is used?

I just noticed, that when I run this code:


import random
import time

delay = random.randint(1,5)

for x in range(1, 101):

    print(x)
    time.sleep(delay)

The delay variable will be always the same since it is determined at the beginning. Is there any way to generate a new random number between 1 and 5 for delay variable in each loop? Now, obviously I could do something like this:


import random

import time

for x in range(1, 101):

    print(x)
    time.sleep(random.randint(1,5))

But I want to use variables since I have a huge code and do not want to go through all of it, if I change something. Many thanks in advance!




Aucun commentaire:

Enregistrer un commentaire