在Python中,如何生成12位数的随机数?是否有任何功能我们可以指定像random.range(12)
这样的范围?
import random
random.randint()
输出应为12位数字的字符串,范围为0-9(允许前导零)。
答案 0 :(得分:29)
直接的做法有什么不对?
>>> import random
>>> random.randint(100000000000,999999999999)
544234865004L
如果你想要它带有前导零,你需要一个字符串。
>>> "%0.12d" % random.randint(0,999999999999)
'023432326286'
修改强>
我自己解决这个问题的方法是这样的:
import random
def rand_x_digit_num(x, leading_zeroes=True):
"""Return an X digit number, leading_zeroes returns a string, otherwise int"""
if not leading_zeroes:
# wrap with str() for uniform results
return random.randint(10**(x-1), 10**x-1)
else:
if x > 6000:
return ''.join([str(random.randint(0, 9)) for i in xrange(x)])
else:
return '{0:0{x}d}'.format(random.randint(0, 10**x-1), x=x)
测试结果:
>>> rand_x_digit_num(5)
'97225'
>>> rand_x_digit_num(5, False)
15470
>>> rand_x_digit_num(10)
'8273890244'
>>> rand_x_digit_num(10)
'0019234207'
>>> rand_x_digit_num(10, False)
9140630927L
速度的计时方法:
def timer(x):
s1 = datetime.now()
a = ''.join([str(random.randint(0, 9)) for i in xrange(x)])
e1 = datetime.now()
s2 = datetime.now()
b = str("%0." + str(x) + "d") % random.randint(0, 10**x-1)
e2 = datetime.now()
print "a took %s, b took %s" % (e1-s1, e2-s2)
速度测试结果:
>>> timer(1000)
a took 0:00:00.002000, b took 0:00:00
>>> timer(10000)
a took 0:00:00.021000, b took 0:00:00.064000
>>> timer(100000)
a took 0:00:00.409000, b took 0:00:04.643000
>>> timer(6000)
a took 0:00:00.013000, b took 0:00:00.012000
>>> timer(2000)
a took 0:00:00.004000, b took 0:00:00.001000
它告诉我们的是什么:
对于大约6000个字符以下的任何数字,我的方法更快 - 有时快得多,但对于较大的数字,arshajii建议的方法看起来更好。
答案 1 :(得分:3)
random.randrange(10**11, 10**12)
。它就像randint
符合range
来自文档:
randrange(self, start, stop=None, step=1, int=<type 'int'>, default=None, maxwidth=9007199254740992L) method of random.Random instance
Choose a random item from range(start, stop[, step]).
This fixes the problem with randint() which includes the
endpoint; in Python this is usually not what you want.
Do not supply the 'int', 'default', and 'maxwidth' arguments.
这实际上就像在执行random.choice(range(10**11, 10**12))
或random.randint(10**1, 10**12-1)
一样。由于它符合与range()
相同的语法,因此它比这两种选择更直观,更清晰
如果允许前导零:
"%012d" %random.randrange(10**12)
答案 2 :(得分:2)
有很多方法可以做到这一点:
import random
rnumber1 = random.randint(10**11, 10**12-1) # randint includes endpoint
rnumber2 = random.randrange(10**11, 10**12) # randrange does not
# useful if you want to generate some random string from your choice of characters
digits = "123456789"
digits_with_zero = digits + "0"
rnumber3 = random.choice(digits) + ''.join(random.choice(digits_with_zero) for _ in range(11))
答案 3 :(得分:2)
由于允许前导零(通过您的评论),您还可以使用:
int(''.join(str(random.randint(0,9)) for _ in xrange(12)))
编辑 :当然,如果你想要一个字符串,你可以省略int
部分:
''.join(str(random.randint(0,9)) for _ in xrange(12))
在我看来,这似乎是最简单的方法。
答案 4 :(得分:1)
from random import randint
def random_with_N_digits(n):
range_start = 10**(n-1)
range_end = (10**n)-1
return randint(range_start, range_end)
print random_with_N_digits(12)
答案 5 :(得分:1)
这可能不是您正在寻找的,但像rstr这样的库让您生成随机字符串。有了你所需要的一切(允许前导0):
import rstr
foo = rstr.digits(12)
答案 6 :(得分:-1)
import random
def generate_random_digits(n):
if not n > 0:
return None
randnum = random.randint(10**n, 2*10**n)
return str(randnum)[-n:]