随机抽样 (numpy.random)

Numpy的随机数例程使用 BitGenerator 和 Generator 的组合来生成伪随机数以创建序列,并使用这些序列从不同的统计分布中进行采样:

  • BitGenerators:生成随机数的对象。这些通常是填充有32或64随机位序列的无符号整数字。
  • 生成器:将来自BitGenerator的随机位序列转换为在指定间隔内遵循特定概率分布(如均匀、正态或二项式)的数字序列的对象。

从Numpy版本1.17.0开始,Generator可以使用许多不同的BitGenerator进行初始化。它暴露了许多不同的概率分布。有关更新的随机Numpy数例程的上下文,请参见NEP 19。遗留的 RandomState 随机数例程仍然可用,但仅限于单个BitGenerator。

为了方便和向后兼容,单个RandomState实例的方法被导入到numpy.Random命名空间中,有关完整列表,请参阅遗留随机生成

快速开始

默认情况下,Generator使用PCG64提供的位, 具有比传统mt19937 RandomState中的随机数字生成器更好的统计属性

# Uses the old numpy.random.RandomState
from numpy import random
random.standard_normal()

尽管随机值是由PCG64生成的,但是Generator可以直接替代RandomState。 Generator包含一个BitGenerator的实例。 可以通过gen.bit_generator来访问。

# As replacement for RandomState(); default_rng() instantiates Generator with
# the default PCG64 BitGenerator.
from numpy.random import default_rng
rg = default_rng()
rg.standard_normal()
rg.bit_generator

Seeds可以传递给任何BitGenerator。 所提供的值通过SeedSequence进行混合,以将可能的seeds序列分布在BitGenerator的更广泛的初始化状态中。 这里使用PCG64并用Generator包裹。

from numpy.random import Generator, PCG64
rg = Generator(PCG64(12345))
rg.standard_normal()

介绍(Introduction)

新的基础架构采用了不同的方法来产生随机数 来自RandomState对象。 随机数生成分为两个组件,一个bit generator和一个random generator

BitGenerator具有有限的职责集。 它管理状态并提供产生随机双精度和随机无符号32位和64位值。

random generator将bit generator提供的流并将其转换为更有用的分布,例如模拟的正常随机值。 这种结构允许备用bit generator,几乎不需要代码重复。

Generator是面向用户的对象,几乎与 RandomState相同。 初始化生成器的规范方法通过PCG64 bit generator作为唯一的参数。

from numpy.random import default_rng
rg = default_rng(12345)
rg.random()

也可以使用 BitGenerator 实例直接实例化Generator。 要使用较旧的MT19937算法,可以直接实例化它 并将其传递给Generator

from numpy.random import Generator, MT19937
rg = Generator(MT19937(12345))
rg.random()

What’s New or Different

::: danger Warning

The Box-Muller method used to produce NumPy’s normals is no longer available in Generator. It is not possible to reproduce the exact random values using Generator for the normal distribution or any other distribution that relies on the normal such as the RandomState.gamma or RandomState.standard_t. If you require bitwise backward compatible streams, use RandomState.

:::

  • The Generator’s normal, exponential and gamma functions use 256-step Ziggurat methods which are 2-10 times faster than NumPy’s Box-Muller or inverse CDF implementations.
  • Optional dtype argument that accepts np.float32 or np.float64 to produce either single or double prevision uniform random variables for select distributions
  • Optional out argument that allows existing arrays to be filled for select distributions
  • random_entropy provides access to the system source of randomness that is used in cryptographic applications (e.g., /dev/urandom on Unix).
  • All BitGenerators can produce doubles, uint64s and uint32s via CTypes (ctypes) and CFFI (cffi). This allows the bit generators to be used in numba.
  • The bit generators can be used in downstream projects via Cython.
  • integers is now the canonical way to generate integer random numbers from a discrete uniform distribution. The rand and randn methods are only available through the legacy RandomState. The endpoint keyword can be used to specify open or closed intervals. This replaces both randint and the deprecated random_integers.
  • random is now the canonical way to generate floating-point random numbers, which replaces RandomState.random_sample, RandomState.sample, and RandomState.ranf. This is consistent with Python’s random.random.
  • All BitGenerators in numpy use SeedSequence to convert seeds into initialized states.

See What’s New or Different for a complete list of improvements and differences from the traditional Randomstate.

Parallel Generation

The included generators can be used in parallel, distributed applications in one of three ways:

Concepts

Features

Original Source

This package was developed independently of NumPy and was integrated in version 1.17.0. The original repo is at https://github.com/bashtage/randomgen.