在Python中random.random()到底如何工作? [英] How exactly does random.random() work in python?

查看:113
本文介绍了在Python中random.random()到底如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于random.random()函数在python中的工作方式,我有些困惑.

I am a bit confused about how the random.random() function works in python.

文档说,它返回下一个随机数浮点数,范围为[0.0,1.0)'. 我了解伪随机数生成器通过对值执行某些操作来工作.通常,此值是生成器生成的先前编号.因此,我认为这就是下一个随机浮点" 在这里的意思. (如果我错了,请纠正我)

The docs say that it 'Return the next random floating point number in the range [0.0, 1.0)'. I understand that pseudo-random number generators work by performing some operation on a value. Generally this value is the previous number generated by the generator. So I think that's what 'next random floating point' means here. (Please correct me if I am wrong)

但是当我看到随机的源代码时在库中,class Random中未定义随机函数.而是,它在class SystemRandom中定义如下(代码的第671行):

But when I saw the source code of the random library, random function is not defined in the class Random. Instead, its defined in the class SystemRandom as follows (line 671 of the code):

 def random(self):
        """Get the next random number in the range [0.0, 1.0)."""
        return (int.from_bytes(_urandom(7), 'big') >> 3) * RECIP_BPF

如果我正确理解这一点,则此函数会使用os.urandom生成一个随机数.根据文档,该文件会从操作系统返回随机字节特定的随机性源.因此,这不会给出下一个"浮点随机数.

If I understand this correctly, this function generates a random number using os.urandom. Which, according to the documentation, returns random bytes from an OS-specific randomness source. So this will not give the 'next' floating point random number.

两者如何连接?还是两个不同的东西?

How are the two connected? or are they two different things?

我在这里很困惑.任何帮助将不胜感激.

I am quite confused here. Any kind of help would be appreciated.

谢谢!

推荐答案

random.random()实际上已定义但是,它只是对C实现的引用.

However, it is just a reference to C implementation.

以下是引文来源:

有关底层Mersenne Twister核心生成器的一般说明:

General notes on the underlying Mersenne Twister core generator:

  • 时间段是2 ** 19937-1.
  • 它是现有的经过最广泛测试的发电机之一.
  • random()方法用C实现,在Python的单个步骤中执行,因此是线程安全的.
  • The period is 2**19937-1.
  • It is one of the most extensively tested generators in existence.
  • The random() method is implemented in C, executes in a single Python step, and is, therefore, threadsafe.

您可能想看一下 Merenne Twister 上的文章.简而言之,生成器的状态与上一个数字"不同,这要复杂得多.因此,您错了……伪随机数生成器通过对值执行某些运算来工作.通常,此值是生成器生成的先前数字».

You probably want to look at the article on Mersenne Twister. To speak briefly, the state of the generator is not the same as "previous number", it is much more complicated thing. So you are wrong in «…pseudo-random number generators work by performing some operation on a value. Generally this value is the previous number generated by the generator».

对于SystemRandom.random(),它与random.random()无关.在Python中,从不同模块导入的具有相同名称的函数可能会有所不同,因此您不能在此处依赖函数的名称.

As for SystemRandom.random(), it is in a way unrelated to random.random(). It is possible in Python that function with the same name imported from different modules are different, so you can't rely on function's name here.

这篇关于在Python中random.random()到底如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆