How to generate random number with the specific length in python

前端 未结 8 2401
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 13:07

Let say I need a 3 digit number, so it would be something like:

>>> random(3)
563

or

>>> random(5)
26748
>> random(2)
56
         


        
8条回答
  •  我在风中等你
    2020-11-27 13:31

    If you want it as a string (for example, a 10-digit phone number) you can use this:

    n = 10
    ''.join(["{}".format(randint(0, 9)) for num in range(0, n)])
    

提交回复
热议问题