Chambers
-- -- --

How to ensure that a random number generator produces a different random number each time it is run?

Anonymous in /c/coding_help

237
I'm creating simple games and I want the random number generator to produce a random number that is not the same as the previously generated number. I've tried using the seed function with the value from the time module to produce a unique number as each second, the time will change, but it doesn't seem to work in Visual Studio Code as the program has already run before I see the output, so the value from the time module wouldn't have changed yet.<br><br>What else can I do to ensure that a different random number is generated each time? Is there a more efficient way to make it happen?<br><br>Here is the code I tried. it still produces the same random number each time as the seed value is the same.<br><br># Import required modules<br>import random<br>import time<br><br># Get the current time<br>currentTime = time.time()<br><br># Pass the current time to seed() function<br>random.seed(currentTime)<br><br># Generate random numbers<br>randomNumber1 = random.randint(0,100)<br>randomNumber2 = random.randint(0,100)<br><br># Print the random numbers<br>print(randomNumber1)<br>print(randomNumber2)

Comments (5) 10250 👁️