Python Forum

Full Version: Help with a random number generator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm interested in designing a script for a random number generator like this:

https://www.programiz.com/python-program...dom-number

However, I want it to generate 6 numbers which I suppose would technically be a 6-digit number for Python. For example, I want it to look something like "589461." Any thoughts?
If you want a 6-digit number, just pick an integer between zero and 999999 and print it out with leading zeros.

>>> import random
>>> print(format(random.randint(0,999999), "06d"))
882729
>>> print(format(random.randint(0,999999), "06d"))
064016
>>> print(format(random.randint(0,999999), "06d"))
918586
so, what have you tried? what would be your strategy to achieve what you want? you can generate 6 single digits and combine them in a 6-digit number. or you can generate a number (0 <= number <= 999999) and left-fill in with 0 if number < 100000 (assuming e.g. 000001 is valid) or 100000 <= number <= 999999 if 000001 is not valid