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).
python
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).
Best Answer
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.