python随机读取字符_python - 创建随机字符串和随机十六进制数字的最轻量级方法...

python - 创建随机字符串和随机十六进制数字的最轻量级方法

创建一个包含30个字符的随机字符串的最轻量级方法是什么,如下所示?

ufhy3skj5nca0d2dfh9hwd2tbk9sw1

和十六进制数字一样,如下所示?

8c6f78ac23b4a7b8c0182d7a89e9b1

xRobot asked 2019-06-20T09:40:08Z

10个解决方案

96 votes

十六进制输出的速度更快。 使用与上述相同的t1和t2:

>>> t1 = timeit.Timer("''.join(random.choice('0123456789abcdef') for n in xrange(30))", "import random")

>>> t2 = timeit.Timer("binascii.b2a_hex(os.urandom(15))", "import os, binascii")

>>> t3 = timeit.Timer("'%030x' % random.randrange(16**30)", "import random")

>>> for t in t1, t2, t3:

... t.timeit()

...

28.165037870407104

9.0292739868164062

5.2836320400238037

t3只对随机模块进行一次调用,不必构建或读取列表,然后使用字符串格式化进行其余操作。

jcdyer answered 2019-06-20T09:43:02Z

61 votes

30位十六进制字符串:

>>> import os,binascii

>>> print binascii.b2a_hex(os.urandom(15))

"c84766ca4a3ce52c3602bbf02ad1f7"

优点是,这直接来自操作系统的随机性,这可能比random()更安全和/或更快,并且您不必为其播种。

wump answered 2019-06-20T09:43:35Z

27 votes

import string

import random

lst = [random.choice(string.ascii_letters + string.digits) for n in xrange(30)]

str = "".join(lst)

print str

ocwbKCiuAJLRJgM1bWNV1TPSH0F2Lb

eemz answered 2019-06-20T09:43:52Z

21 votes

解决方案比这里的解决方案快得多:

timeit("'%0x' % getrandbits(30 * 4)", "from random import getrandbits")

0.8056681156158447

Kurt Spindler answered 2019-06-20T09:44:17Z

20 votes

在Py3.6 +中,另一种选择是使用新标准secrets模块:

>>> import secrets

>>> secrets.token_hex(15)

'8d9bad5b43259c6ee27d9aadc7b832'

>>> secrets.token_urlsafe(22) # may include '_-' unclear if that is acceptable

'teRq7IqhaRU0S3euX1ji9f58WzUkrg'

AChampion answered 2019-06-20T09:44:42Z

14 votes

注意:random.choice(string.hexdigits)不正确,因为string.hexdigits返回0123456789abcdefABCDEF(小写和大写),因此您将得到偏差结果,十六进制数字'c'可能显示为数字'7'的两倍。 相反,只需使用random.choice('0123456789abcdef')。

yaronf answered 2019-06-20T09:45:08Z

6 votes

另一种方法:

from Crypto import Random

import binascii

my_hex_value = binascii.hexlify(Random.get_random_bytes(30))

重点是:字节值始终等于十六进制值。

dsgdfg answered 2019-06-20T09:45:41Z

4 votes

单线功能:

import random

import string

def generate_random_key(length):

return ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(length))

print generate_random_key(30)

Boubakr answered 2019-06-20T09:46:07Z

2 votes

顺便说一句,这是使用binascii.b2a_hex()对已建议的两种方法的结果:

使用binascii.b2a_hex():

>>> t1 = timeit.Timer("''.join(random.choice(string.hexdigits) for n in xrange(30))", "import random, string")

>>> t1.timeit()

69.558588027954102

使用binascii.b2a_hex():

>>> t2 = timeit.Timer("binascii.b2a_hex(os.urandom(15))", "import os, binascii")

>>> t2.timeit()

16.288421154022217

David Narayan answered 2019-06-20T09:46:43Z

2 votes

与jcdyer提到的相比,速度更快。 这需要他最快的方法的约50%。

from numpy.random.mtrand import RandomState

import binascii

rand = RandomState()

lo = 1000000000000000

hi = 999999999999999999

binascii.b2a_hex(rand.randint(lo, hi, 2).tostring())[:30]

>>> timeit.Timer("binascii.b2a_hex(rand.randint(lo,hi,2).tostring())[:30]", \

... 'from __main__ import lo,hi,rand,binascii').timeit()

1.648831844329834 <-- this is on python 2.6.6

2.253110885620117 <-- this on python 2.7.5

如果你想在base64:

binascii.b2a_base64(rand.randint(lo, hi, 3).tostring())[:30]

您可以更改传递给randint(last arg)的size参数,以根据您的要求改变输出长度。 所以,对于60个字符:

binascii.b2a_hex(rand.randint(lo, hi, 4).tostring())[:60]

Ethan answered 2019-06-20T09:47:25Z

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值