Python: How to generate a 12-digit random number?

前端 未结 6 1348
予麋鹿
予麋鹿 2021-01-04 03:39

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
ran         


        
6条回答
  •  既然无缘
    2021-01-04 04:19

    Since leading zeros are allowed (by your comment), you could also use:

    int(''.join(str(random.randint(0,9)) for _ in xrange(12)))
    

    EDIT: Of course, if you want a string, you can just leave out the int part:

    ''.join(str(random.randint(0,9)) for _ in xrange(12))
    

    This seems like the most straightforward way to do it in my opinion.

提交回复
热议问题