[英]Can I generate authentic random number with python?
I'm learning random module of python. And I know it generates pseudo random number.我正在学习 python 的随机模块。我知道它会生成伪随机数。 Which its core idea is to use a high-frequency clock as a seed and then use a function to produce a 'looking-like random number.
它的核心思想是使用高频时钟作为种子,然后使用 function 产生一个“看起来像随机数”。
As I know it's even impossible to produce authentic random number in the real world.据我所知,在现实世界中甚至不可能产生真实的随机数。
But I know Unix random producer has introduced some other factors such as the parameter of mouse-movement track, the IO response time, to introduce uncertainty to its random number producer function. Through which we can get a better random number than normal pseudo random number.但是我知道Unix随机生成器引入了一些其他因素,比如鼠标移动轨迹参数,IO响应时间,给它的随机数生成器function引入了不确定性。通过它我们可以得到比普通伪随机数更好的随机数. It's much more harder to predict.
这更难预测。
So, is there a way that in python we can produce such a random number, or maybe import a good third party library?那么,有没有办法在 python 中产生这样一个随机数,或者导入一个好的第三方库?
The documentation for the random
module has this to say: random
模块的文档是这样说的:
Warning : The pseudo-random generators of this module should not be used for security purposes.
警告:该模块的伪随机生成器不应用于安全目的。 Use
os.urandom()
orSystemRandom
if you require a cryptographically secure pseudo-random number generator.使用
os.urandom()
或SystemRandom
如果你需要加密安全伪随机数发生器。
Truly random numbers can be generated from真正的随机数可以从
https://pypi.python.org/pypi/quantumrandom/ https://pypi.python.org/pypi/quantumrandom/
pip install quantumrandom
Currently you are limited to blocks of 1024 but with a bit of simple programming and a little bit of time you will be able to extend this limit to a large enough sample for most applications.目前,您仅限于 1024 个块,但通过一些简单的编程和一点时间,您将能够将此限制扩展到足够大的样本,适用于大多数应用程序。
https://www.random.org/integers/ https://www.random.org/integers/
https://api.random.org/json-rpc/1/ https://api.random.org/json-rpc/1/
This website generates random numbers through atmospheric white noise, which is better than pseudo random numbers for use with development, You can also use there API for automated random numbers (Though it won't be free for long as it's currently in beta.)该网站通过大气白噪声生成随机数,这比用于开发的伪随机数要好,您也可以使用那里的 API 自动随机数(尽管它目前处于测试阶段不会免费。)
Another method of obtaining true random numbers is through the quantum random number generator, http://photonics.anu.edu.au/qoptics/Research/qrng.php .另一种获得真随机数的方法是通过量子随机数发生器, http://photonics.anu.edu.au/qoptics/Research/qrng.php 。
To reiterate what someone said earlier, you should avoid using computationally made pseudo random numbers for security purposes.重申之前有人说过的话,出于安全目的,您应该避免使用计算生成的伪随机数。
Python has nothing that allows you to generate "truly random" or "authentic random" numbers, in the sense that they are uniformly distributed and independent of everything else (especially the latter). Python 没有任何东西可以让您生成“真正随机”或“真实随机”数字,因为它们均匀分布并且独立于其他所有内容(尤其是后者)。
In any case, the distinction between "pseudorandom" and "truly random" numbers is not what applications care about.无论如何,“伪随机”和“真正随机”数字之间的区别并不是应用程序关心的。 Rather, the requirements for randomness depend on the application, and you didn't really specify what kind of application you have in mind.
相反,随机性的要求取决于应用程序,并且您并没有真正指定您想要什么样的应用程序。 For example, in general:
例如,一般来说:
secrets
module or random.SystemRandom
. secrets
模块或random.SystemRandom
。numpy.random.Generator
or random.Random
. numpy.random.Generator
或random.Random
。 See also these questions:另请参阅以下问题:
I think this works:我认为这有效:
def get_truly_random_seed_through_os() -> int:
"""
Usually the best random sample you could get in any programming language is generated through the operating system.
In Python, you can use the os module.
source: https://stackoverflow.com/questions/57416925/best-practices-for-generating-a-random-seeds-to-seed-pytorch/57416967#57416967
"""
RAND_SIZE = 4
random_data = os.urandom(
RAND_SIZE
) # Return a string of size random bytes suitable for cryptographic use.
random_seed: int = int.from_bytes(random_data, byteorder="big")
return int(random_seed)
if you only want different seeds you can do:如果你只想要不同的种子,你可以这样做:
def get_different_pseudo_random_seed_every_time_using_time() -> int:
import random
import time
# random.seed(int(time.time()))
seed: int = int(time.time())
return seed
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.