emonkak / random

随机数生成器库

v3.0.0 2020-06-04 01:10 UTC

This package is auto-updated.

Last update: 2024-09-04 10:30:02 UTC


README

Build Status Coverage Status

此库提供伪随机数生成器和概率分布。

要求

  • PHP 7.1 或更高版本

许可证

MIT 许可证

示例

use Emonkak\Random\Engine\MT19937Engine;
use Emonkak\Random\Distribution\NormalDistribution;

$seed = 100;  // Initial seed
$engine = new MT19937Engine($seed);  // 32bit Mersenne Twister engine
$distribution = new NormalDistribution(0, 1);  // Standard normal distribution

// Generate a random number with the normal distribution.
$distribution->generate($engine);

引擎

  • KnuthBEngine

  • 线性同余引擎

  • MinstdRand0Engine

  • MinstdRandEngine

  • MT19937Engine

    基于 Mersenne Twister 的随机生成器引擎。它与内置的 mt_rand() 完全兼容。

     // Also, the initial seed algorithm is full-compatible to the built-in `mt_srand()`
     $engine = new MT19937Engine(/* $seed */);
    
     // Get a next random number from the current generator state.
     $number = $engine->next();  // as int
     $number = $engine->nextDouble();  // as float
    
     // Get the minimum and maximum number which generate a value by the engine.
     $minimum = $engine->min();
     $maximum = $engine->max();
    
     // Iterate the generator engine.
     foreach (new LimitIterator($engine, 0, 100) as $n) {
     }
  • MTRandWrapper

    内置 mt_rand() 的包装器。

  • ShuffleOrderEngine

  • XorShift128Engine

    根据 Xorshift 128 位算法的随机生成器引擎。

分布

  • BernoulliDistribution
  • 二项分布
  • 离散分布
  • 分布迭代器
  • 指数分布
  • 伽玛分布
  • 几何分布
  • 对数正态分布
  • 正态分布
  • 分段常数分布
  • 分段线性分布
  • 均匀整数分布
  • 均匀实数分布