![]() |
Help with a random number generator - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help with a random number generator (/thread-27756.html) |
Help with a random number generator - dpcalder - Jun-20-2020 I'm interested in designing a script for a random number generator like this: https://www.programiz.com/python-programming/examples/random-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? RE: Help with a random number generator - bowlofred - Jun-20-2020 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 RE: Help with a random number generator - buran - Jun-20-2020 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 |