Python中是否已有生成仅使用数字的强随机序列的包?

1 投票
3 回答
545 浏览
提问于 2025-04-18 09:03

我在用Python的uuid库来生成随机代码,但它生成的代码里有数字和字母。我想知道Python里有没有现成的库,可以只生成强随机的数字序列?(现在我有两个选择来生成36位的数字序列:一个是每次调用random()来生成每个数字,另一个是把字母转换成数字,但我不确定这样生成的随机性是否足够强。)

3 个回答

1

我不太确定的UUID有多强,那为什么不直接依赖操作系统来帮你生成随机数呢?

import os
32_bytes_of_random = os.urandom(32)

它的度取决于操作系统使用的随机数的强度,但我不太确定你能有多大把握。此外,如果你想把这些“随机”数据仅用数字表示,而不是字母和数字混合,那就需要更多的字符了。

如果你在Python中寻找真正的加密随机数,可以看看cryptography.io上的内容。在2014年的PyCon上,有个很棒的讲座可以在Youtube上找到。

2

是的,uuid。UUID(通用唯一标识符)是用十六进制字符串表示的,中间用短横线分隔。所以你可以通过去掉UUID中的所有短横线,把它变成一个连续的十六进制表示,然后再把它转换成十进制来得到一个十进制的UUID:

hex_uuid = uuid.uuid4()
hex_uuid = str(hex_uuid).replace("-", "")
decimal_uuid = str(int(hex_uuid, 16))

一行代码:

decimal_uuid = str(int(str(uuid.uuid4()).replace("-", ""), 16))

编辑 Duncan的回答提到了一种更简单的方法来进行上述转换。

不过,你要记住UUID并不是完全“随机”的,它们只是保证是唯一的(好吧,直到未来的某个时刻)。

如果你需要加密级别的强随机数(也就是非常强的随机数),那么你应该使用一个加密库,比如M2Crypto,这是基于OpenSSL的:

from M2Crypto.Rand import rand_bytes, rand_seed

# Maybe seed with rand_seed("seed with a very random string, not like this")
decimal_id = str(int(rand_bytes(64).encode("hex"), 16))

这保证是加密级别强的,但不保证是唯一的(也就是说,生成相同的ID两次是可能的,尽管在某种定义下这种情况极不可能发生)。

9

Python返回的uuid实际上是一个类,所以你可以很方便地以不同的格式获取它:

>>> import uuid
>>> uuid.uuid4().int
27168693431722041803402778536822837233

这里是UUID类型的在线帮助文档。你需要关注的是int属性。

>>> help(uuid.UUID)
Help on class UUID in module uuid:

class UUID(builtins.object)
 |  Instances of the UUID class represent UUIDs as specified in RFC 4122.
 |  UUID objects are immutable, hashable, and usable as dictionary keys.
 |  Converting a UUID to a string with str() yields something in the form
 |  '12345678-1234-1234-1234-123456789abc'.  The UUID constructor accepts
 |  five possible forms: a similar string of hexadecimal digits, or a tuple
 |  of six integer fields (with 32-bit, 16-bit, 16-bit, 8-bit, 8-bit, and
 |  48-bit values respectively) as an argument named 'fields', or a string
 |  of 16 bytes (with all the integer fields in big-endian order) as an
 |  argument named 'bytes', or a string of 16 bytes (with the first three
 |  fields in little-endian order) as an argument named 'bytes_le', or a
 |  single 128-bit integer as an argument named 'int'.
 |  
 |  UUIDs have these read-only attributes:
 |  
 |      bytes       the UUID as a 16-byte string (containing the six
 |                  integer fields in big-endian byte order)
 |  
 |      bytes_le    the UUID as a 16-byte string (with time_low, time_mid,
 |                  and time_hi_version in little-endian byte order)
 |  
 |      fields      a tuple of the six integer fields of the UUID,
 |                  which are also available as six individual attributes
 |                  and two derived attributes:
 |  
 |          time_low                the first 32 bits of the UUID
 |          time_mid                the next 16 bits of the UUID
 |          time_hi_version         the next 16 bits of the UUID
 |          clock_seq_hi_variant    the next 8 bits of the UUID
 |          clock_seq_low           the next 8 bits of the UUID
 |          node                    the last 48 bits of the UUID
 |  
 |          time                    the 60-bit timestamp
 |          clock_seq               the 14-bit sequence number
 |  
 |      hex         the UUID as a 32-character hexadecimal string
 |  
 |      int         the UUID as a 128-bit integer
 |  
 |      urn         the UUID as a URN as specified in RFC 4122
 |  
 |      variant     the UUID variant (one of the constants RESERVED_NCS,
 |                  RFC_4122, RESERVED_MICROSOFT, or RESERVED_FUTURE)
 |  
 |      version     the UUID version number (1 through 5, meaningful only
 |                  when the variant is RFC_4122)
 |  

撰写回答