In Python, how to generate a 12-digit random number? Is there any function where we can specify a range like random.range(12)?
import random
random.randint()
The output should be a string with 12 digits in the range 0-9 (leading zeros allowed).
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Whats wrong with a straightforward approach?
And if you want it with leading zeros, you need a string.
Edit:
My own solution to this problem would be something like this:
Testing Results:
Timing methods for speed:
Speed test results:
What it tells us:
For any digit under around 6000 characters in length my method is faster – sometimes MUCH faster, but for larger numbers the method suggested by arshajii looks better.