I took this line from online to make a random 4 digit number
number = str(random.randint(0,9999))
but sometimes it give me random 3 digit number why?
Is their a better way to make a random multiple digit number in python
I took this line from online to make a random 4 digit number
number = str(random.randint(0,9999))
but sometimes it give me random 3 digit number why?
Is their a better way to make a random multiple digit number in python
Because you've asked for a number between 0 and 9999 inclusive. That includes single, double and triple digit numbers as well.
If you want only 4 digit numbers start at 1000:
number = str(random.randint(1000, 9999))
See the randint()
documentation.
If you want 4 digits that could include leading zeroes then you use random.choices()
:
from string import digits
number = ''.join(random.choices(digits, k=4))
which picks 4 digits with replacement and joins them into a string.