http5/random

具有数十种有用功能和几个随机数源的决定性伪随机数生成器库

维护者

详细信息

github.com/http5/random

源代码

0.4 2019-04-04 17:23 UTC

This package is not auto-updated.

Last update: 2024-09-28 09:20:49 UTC


README

此组类提供对具有相同通用API的不同伪随机生成器的基本抽象。它还包含许多有用的辅助方法,如加权随机、文本生成、洗牌、数组函数等。

警告:这些PRNGs 非加密安全(mt_rand()也是如此)

Latest Stable Version License Build Status Scrutinizer Code Quality Code Coverage SensioLabsInsight

为什么不使用mt_rand()?

PHP内建的mt_rand()和rand()是全局函数,因此无法创建多个具有不同预定义种子的生成器并同时使用。也无法控制随机序列的当前状态。

特性

  • 具有可选种子的决定性随机生成器
  • 正确的均匀分布
  • 基于Ziggurat算法的正态分布随机数采样器
  • 可扩展架构:轻松添加自己的随机数源
  • 种子可以是任意长度的字符串
  • 可以在任何时间保存和恢复当前PRNG的状态,因此可以从已知状态重新播放序列
  • 不同的随机类型:int、float、boolean
  • 加权随机:根据权重随机选择数组键,加权洗牌
  • 随机二进制数据生成
  • 从指定字符列表生成随机文本,可选支持多字节
  • 数组方法,是array_rand、shuffle的替代方案

PRNG源

  • XorShiftRand 快速且质量良好的随机生成器,基于128位状态的XorShift+算法。在大多数情况下都应使用。

  • MtRand Mersenne twister算法的内建mt_rand变体的纯PHP实现。具有相同int种子的两种方法的随机序列将完美匹配,因此该生成器与mt_rand()完全兼容,可以用作替代方案。

  • HashRand 证明md5散列均匀分布,可以用作伪随机数的源。相当快速且直接。

示例

//////// BASIC USAGE ////////

// Create xorshift random generator with default random seed
$rnd = new XorShiftRand();

// Default random value between 0 and GeneratorClass::INT_MAX
$value = $rnd->random();

// Random value within given range (inclusive)
$value = $rnd->random(15, 12333);

// Negative numbers allowed
$value = $rnd->random(-128, 128);

// Random unsigned int, size depends on underlying generator
$int = $rnd->randomInt();

// Random float in range 0 <= $val <= 1
$float = $rnd->randomFloat();

// True or false
$bool = $rnd->randomBool();

//////// STATE CONTROL ////////

// Read current seed
$seed = $rnd->getSeed();

// Set new seed, generator will be reinitialized
$rnd->setSeed('some new seed');

// .... several generator calls later ...

// Save state for future use
$state = $rnd->getState();

// .... several generator calls later ...

// Set saved state and restore generator to known state
$state = $rnd->setState($state); 

// .... same calls to random methods as before will produce exactly the same output

// Reset generator to initial state with current seed ('some new seed')
$rnd->reset();

//////// STRING METHODS ////////

// Generate string with 256 random bytes
$bytes = $rnd->randomData(256);

// Generate random string from default (ALNUM) characters list with length = 17
$str = $rnd->randomString(17);

// Generate random string from numbers (0-9) with length = 99
$str = $rnd->randomString(99, $rnd::CL_NUM);

// Generate lowercase hex string with length = 32
$str = $rnd->randomString(32, $rnd::CL_HEXLC);

// Generate string from custom characters list
$str = $rnd->randomString(16, 'ABCDefgh#$%^');

// If custom list contains multi-byte characters, $mb flag must be set
$str = $rnd->randomString(16, 'АБВГДЕЁЖЗ', true);

//////// ARRAY METHODS ////////

// Get random key from array (0, 1, 2 or 'four')
$key = $rnd->arrayRand([10, 20, 30, 'four' => 40]);

// Get random value from array (10, 20, 30 or 4)
$value = $rnd->arrayRandValue([10, 20, 30, 'four' => 40]);

// Shuffle array. New numeric keys will be assigned
$array = [1, 2, 'key' => 3, 4, 'assoc' => 5, 6, 7];
$rnd->arrayShuffle($array);

// Shuffle array and maintain key => value associations
$array = [1, 2, 'key' => 3, 4, 'assoc' => 5, 6, 7];
$rnd->arrayShuffleAssoc($array);

//////// WEIGHTED RANDOM ////////

// Array in "key => weight" form must be specified
$array = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'four2' => 4];

// Get random key from array by weights. 
// So 'one' will appear less likely than 'four'\'four2'
$key = $rnd->arrayWeightRand($array);

// Shuffle array using weights. 
// So 'four'\'four2' likely will appear earlier than 'one' in resulting array
$rnd->arrayWeightShuffle($array);

更多信息