PHPackages                             savvot/random - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. savvot/random

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

savvot/random
=============

Deterministic pseudo-random generators library with dozens of useful functions and several sources of randomness

v0.3.0(10y ago)21243.7k↓17.4%4[1 issues](https://github.com/savvot/random/issues)5MITPHP

Since Sep 7Pushed 10y ago1 watchersCompare

[ Source](https://github.com/savvot/random)[ Packagist](https://packagist.org/packages/savvot/random)[ RSS](/packages/savvot-random/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (4)Used By (5)

PHP pseudo random generators library
====================================

[](#php-pseudo-random-generators-library)

This set of classes provides basic abstraction around different pseudo random generators with the same generic API. Also it contains many useful helper methods like weighted random, text generation, shuffling, array functions, etc.

WARNING: This PRNGs are **non cryptographically secure** (mt\_rand() too)

[![Latest Stable Version](https://camo.githubusercontent.com/2f67544f02ed8d59a0fe7a72f8f16484bee4d7c6c51d040ad1269f125726d1bd/68747470733a2f2f706f7365722e707567782e6f72672f736176766f742f72616e646f6d2f762f737461626c65)](https://packagist.org/packages/savvot/random)[![License](https://camo.githubusercontent.com/4f9bcf0f5e7512e4c114f3603520e7aaff8d7137198b24d87b86314d00f0069e/68747470733a2f2f706f7365722e707567782e6f72672f736176766f742f72616e646f6d2f6c6963656e7365)](https://packagist.org/packages/savvot/random)[![Build Status](https://camo.githubusercontent.com/e6ae2d0541d904a7de4ab4da8cc3fef762500c59f60a7fc2e03c2ad4c7dfd9eb/68747470733a2f2f7472617669732d63692e6f72672f736176766f742f72616e646f6d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/savvot/random)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/6cb4b0556214cb028d25137ed7c69f27e90b15123f758fdbc334fb3e7c56758b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f736176766f742f72616e646f6d2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/savvot/random/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/235cab5c43d20abe13c6425ae601f8cd2ff12780dccd3826be51383ce3fef16a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f736176766f742f72616e646f6d2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/savvot/random/?branch=master)[![SensioLabsInsight](https://camo.githubusercontent.com/cfe4632f58b622b87425601a628d72f8cde68f82c3df84615940a87089f9a383/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f38313733383136372d353164332d343061622d383832392d3633313430363438383735652f6d696e692e706e67)](https://insight.sensiolabs.com/projects/81738167-51d3-40ab-8829-63140648875e)

Why not mt\_rand()?
-------------------

[](#why-not-mt_rand)

PHP builtin mt\_rand() and rand() are global functions, so it is not possible to create several generators with different predefined seeds and use them simultaneously. There is also no control over current state of random sequences.

Features
--------

[](#features)

- Deterministic random generators with optional seeds
- Correct uniform distribution
- Gaussian sampler for normally distributed random numbers based on **Ziggurat algorithm**
- Extensible architecture: it is easy to add your own source of randomness
- Seeds can be **strings** of any length you want
- Possibility to save and restore current PRNG's state at any time, so one can **replay** the sequence from known state
- Different random types: int, float, boolean
- **Weighted random**: random array key by weights, weighted shuffle
- Random binary data generation
- Random text generation from specified characters list with optional multi-byte support
- Array methods, alternatives to array\_rand, shuffle

PRNG sources
------------

[](#prng-sources)

- **XorShiftRand**Fast and good quality random generator, based on XorShift+ algorithm with 128bit state. Should be used in most cases.
- **MtRand**Pure PHP implementation of builtin mt\_rand variation of Mersenne twister algorithm. Random sequences from both methods with same int seed will match perfectly, therefore this generator is fully compatible with mt\_rand() and can be used as replacement.
- **HashRand**Proof-of-concept that md5 hash is uniformly distributed and can be used as source for pseudo random numbers. Pretty fast and straightforward.

Examples
--------

[](#examples)

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

More info
---------

[](#more-info)

- PRNG: [https://en.wikipedia.org/wiki/Pseudorandom\_number\_generator](https://en.wikipedia.org/wiki/Pseudorandom_number_generator)
- XorShift:
- Mersenne Twister: [https://en.wikipedia.org/wiki/Mersenne\_Twister](https://en.wikipedia.org/wiki/Mersenne_Twister)
- Some PRNGs benchmark:
- Normal distribution: [https://en.wikipedia.org/wiki/Normal\_distribution](https://en.wikipedia.org/wiki/Normal_distribution)
- Ziggurat algorithm: [https://en.wikipedia.org/wiki/Ziggurat\_algorithm](https://en.wikipedia.org/wiki/Ziggurat_algorithm)

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity44

Moderate usage in the ecosystem

Community16

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~10 days

Total

3

Last Release

3887d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e4c4a5ae8c377579c4687d94b64288652db566dcd367f199feb83452366c2acf?d=identicon)[savvot](/maintainers/savvot)

---

Top Contributors

[![savvot](https://avatars.githubusercontent.com/u/4719486?v=4)](https://github.com/savvot "savvot (26 commits)")

---

Tags

randomphpPRNGRNGrandom number generatorpseudo-randomrnd

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/savvot-random/health.svg)

```
[![Health](https://phpackages.com/badges/savvot-random/health.svg)](https://phpackages.com/packages/savvot-random)
```

###  Alternatives

[gladcodes/keygen

A fluent PHP random key generator.

119668.9k2](/packages/gladcodes-keygen)[delight-im/random

The most convenient way to securely generate anything random in PHP

2345.4k](/packages/delight-im-random)[ruafozy/mersenne-twister

Pure-PHP Mersenne Twister

221.4M12](/packages/ruafozy-mersenne-twister)[skywarth/chaotic-schedule

Randomize scheduled command execution time and date intervals

12142.3k](/packages/skywarth-chaotic-schedule)[sinergi/token

PHP library to generate random strings

188.7k9](/packages/sinergi-token)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
