emonkak/random

A random number generator library

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

This package is auto-updated.

Last update: 2024-12-04 11:10:42 UTC


README

Build Status Coverage Status

This library provides pseudo-random number generators and probability distributions.

Requirements

  • PHP 7.1 or higher

Licence

MIT Licence

Example

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);

Engine

  • KnuthBEngine

  • LinearCongruentialEngine

  • MinstdRand0Engine

  • MinstdRandEngine

  • MT19937Engine

    The random generator engine according to Mersenne Twister. It is full-compatible to the built-in 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

    The wrapper for the built-in mt_rand().

  • ShuffleOrderEngine

  • XorShift128Engine

    The random generator engine according to Xorshift 128 bit algorithm.

Distribution

  • BernoulliDistribution
  • BinomialDistribution
  • DiscreteDistribution
  • DistributionIterator
  • ExponentialDistribution
  • GammaDistribution
  • GeometricDistribution
  • LogNormalDistribution
  • NormalDistribution
  • PiecewiseConstantDistribution
  • PiecewiseLinerDistribution
  • UniformIntDistribution
  • UniformRealDistribution