python - 使用 Python 创建一个 "uncrackable" "random"数字

标签 python

据说Python的随机数生成器依赖

time

这意味着如果我想创建一个这样的随机数

23987429038409238409283

并将其存储到浏览器cookie中以进行“身份验证”

有可能有人可以根据“时间”找到这个数字。

所以问题是,我如何创建一个随机数,让其他对代码了解很多的人猜不到。 ?

最佳答案

如果您的系统可用,您可以使用 random.SystemRandom:

http://docs.python.org/2/library/random.html#random.SystemRandom

Class that uses the os.urandom() function for generating random numbers from sources provided by the operating system. Not available on all systems. Does not rely on software state and sequences are not reproducible.

http://docs.python.org/2/library/os.html#os.urandom

Return a string of n random bytes suitable for cryptographic use.

This function returns random bytes from an OS-specific randomness source. The returned data should be unpredictable enough for cryptographic applications, though its exact quality depends on the OS implementation.

例如

>>> import sys
>>> import random
>>> rng = random.SystemRandom()
>>> rng.random()
0.7195432667967437
>>> rng.randint(0, sys.maxint)
3614556690529452993

关于python - 使用 Python 创建一个 "uncrackable" "random"数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18047338/

相关文章:

python - 任务队列停止工作

python - 如何在 PyCharm 中为 SQLAlchemy 配置外部文档?

python - 从任务中调用 Java/Scala 函数

python - 无法让 XPath 单击 selenium 中的弹出窗口

python - C++ 中的通用异常处理

python 3 : Print multiple lines to one line

Windows 上的 Python : "Access is denied" sometimes when calling win32gui. SetForegroundWindow()

python - Boto3 ClientMethods 说明

python - wx.App 的目的是仅显示一个框架就足够了

python - OpenSSL.crypto.X509.sign() 抛出 "' 字节的对象没有属性 'encode'“