PHPackages                             ircmaxell/random-lib - 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. ircmaxell/random-lib

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

ircmaxell/random-lib
====================

A Library For Generating Secure Random Numbers

v1.2.0(9y ago)84130.2M—3.2%114[19 issues](https://github.com/ircmaxell/RandomLib/issues)[4 PRs](https://github.com/ircmaxell/RandomLib/pulls)20MITPHPPHP &gt;=5.3.2

Since Jul 30Pushed 7y ago35 watchersCompare

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

READMEChangelogDependencies (4)Versions (6)Used By (20)

RandomLib
=========

[](#randomlib)

[![Build Status](https://camo.githubusercontent.com/95cfc1ab0a492f0f4a11a3aa5ac8290523c739300deed99e93048e8071272bb7/68747470733a2f2f7472617669732d63692e6f72672f6972636d6178656c6c2f52616e646f6d4c69622e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/ircmaxell/RandomLib)

A library for generating random numbers and strings of various strengths.

This library is useful in security contexts.

Install
-------

[](#install)

Via Composer

```
$ composer require ircmaxell/random-lib
```

Usage
-----

[](#usage)

### Factory

[](#factory)

A factory is used to get generators of varying strength:

```
$factory = new RandomLib\Factory;
$generator = $factory->getGenerator(new SecurityLib\Strength(SecurityLib\Strength::MEDIUM));
```

A factory can be configured with additional mixers and sources but can be used out of the box to create both medium and low strength generators.

Convenience methods are provided for creating high, medium, and low strength generators. Example:

```
$generator = $factory->getMediumStrengthGenerator();
```

#### $factory-&gt;getLowStrengthGenerator()

[](#factory-getlowstrengthgenerator)

Convenience method to get a low strength random number generator.

Low Strength should be used anywhere that random strings are needed in a non-cryptographical setting. They are not strong enough to be used as keys or salts. They are however useful for one-time use tokens.

#### $factory-&gt;getMediumStrengthGenerator()

[](#factory-getmediumstrengthgenerator)

Convenience method to get a medium strength random number generator.

Medium Strength should be used for most needs of a cryptographic nature. They are strong enough to be used as keys and salts. However, they do take some time and resources to generate, so they should not be over-used

#### $factory-&gt;getHighStrengthGenerator()

[](#factory-gethighstrengthgenerator)

Convenience method to get a high strength random number generator.

High Strength keys should ONLY be used for generating extremely strong cryptographic keys. Generating them is very resource intensive and may take several minutes or more depending on the requested size.

**There are currently no mixers shipped with this package that are capable of creating a high space generator. This will not work out of the box!**

### Generator

[](#generator)

A generator is used to generate random numbers and strings.

Example:

```
// Generate a random string that is 32 bytes in length.
$bytes = $generator->generate(32);

// Generate a whole number between 5 and 15.
$randomInt = $generator->generateInt(5, 15);

// Generate a 32 character string that only contains the letters
// 'a', 'b', 'c', 'd', 'e', and 'f'.
$randomString = $generator->generateString(32, 'abcdef');
```

#### $generator-&gt;generate($size)

[](#generator-generatesize)

Generate a random byte string of the requested size.

#### $generator-&gt;generateInt($min = 0, $max = PHP\_INT\_MAX)

[](#generator-generateintmin--0-max--php_int_max)

Generate a random integer with the given range. If range (`$max - $min`) is zero, `$max` will be used.

#### $generator-&gt;generateString($length, $characters = '')

[](#generator-generatestringlength-characters--)

Generate a random string of specified length.

This uses the supplied character list for generating the new result string. The list of characters should be specified as a string containing each allowed character.

If no character list is specified, the following list of characters is used:

```
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/

```

**Examples:**

```
// Give the character list 'abcdef':
print $generator->generateString(32, 'abcdef')."\n";

// One would expect to receive output that only contained those
// characters:
//
// adaeabecfbddcdaeedaedfbbcdccccfe
// adfbfdbfddadbfcbbefebcacbefafffa
// ceeadbcabecbccacdcaabbdccfadbafe
// abadcffabdcacdbcbafcaecabafcdbbf
// dbdbddacdeaceabfaefcbfafebcacdca
```

License
-------

[](#license)

MIT, see LICENSE.

Community
---------

[](#community)

If you have questions or want to help out, join us in the **\#php.security**channel on **irc.freenode.net**.

Security Vulnerabilities
========================

[](#security-vulnerabilities)

If you have found a security issue, please contact the author directly at .

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity73

Solid adoption and visibility

Community48

Growing community involvement

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 50% 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 ~378 days

Total

4

Last Release

3540d ago

### Community

Maintainers

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

---

Top Contributors

[![ircmaxell](https://avatars.githubusercontent.com/u/660654?v=4)](https://github.com/ircmaxell "ircmaxell (36 commits)")[![simensen](https://avatars.githubusercontent.com/u/191200?v=4)](https://github.com/simensen "simensen (11 commits)")[![ramsey](https://avatars.githubusercontent.com/u/42941?v=4)](https://github.com/ramsey "ramsey (4 commits)")[![flip111](https://avatars.githubusercontent.com/u/2244480?v=4)](https://github.com/flip111 "flip111 (4 commits)")[![lavoiesl](https://avatars.githubusercontent.com/u/1216046?v=4)](https://github.com/lavoiesl "lavoiesl (3 commits)")[![davedevelopment](https://avatars.githubusercontent.com/u/61351?v=4)](https://github.com/davedevelopment "davedevelopment (1 commits)")[![dol](https://avatars.githubusercontent.com/u/41777?v=4)](https://github.com/dol "dol (1 commits)")[![ezimuel](https://avatars.githubusercontent.com/u/475967?v=4)](https://github.com/ezimuel "ezimuel (1 commits)")[![abacaphiliac](https://avatars.githubusercontent.com/u/1656273?v=4)](https://github.com/abacaphiliac "abacaphiliac (1 commits)")[![hansott](https://avatars.githubusercontent.com/u/3886384?v=4)](https://github.com/hansott "hansott (1 commits)")[![LukasReschke](https://avatars.githubusercontent.com/u/878997?v=4)](https://github.com/LukasReschke "LukasReschke (1 commits)")[![Maks3w](https://avatars.githubusercontent.com/u/1301698?v=4)](https://github.com/Maks3w "Maks3w (1 commits)")[![sarciszewski](https://avatars.githubusercontent.com/u/3710836?v=4)](https://github.com/sarciszewski "sarciszewski (1 commits)")[![sasezaki](https://avatars.githubusercontent.com/u/42755?v=4)](https://github.com/sasezaki "sasezaki (1 commits)")[![gajus](https://avatars.githubusercontent.com/u/973543?v=4)](https://github.com/gajus "gajus (1 commits)")[![bcrowe](https://avatars.githubusercontent.com/u/752603?v=4)](https://github.com/bcrowe "bcrowe (1 commits)")[![bobdenotter](https://avatars.githubusercontent.com/u/1833361?v=4)](https://github.com/bobdenotter "bobdenotter (1 commits)")[![bolmstedt](https://avatars.githubusercontent.com/u/1132901?v=4)](https://github.com/bolmstedt "bolmstedt (1 commits)")[![cs278](https://avatars.githubusercontent.com/u/17377?v=4)](https://github.com/cs278 "cs278 (1 commits)")

---

Tags

randomcryptographyrandom-numbersrandom-strings

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[paragonie/random_compat

PHP 5.x polyfill for random\_bytes() and random\_int() from PHP 7

8.2k655.0M405](/packages/paragonie-random-compat)[paragonie/random-lib

A Library For Generating Secure Random Numbers

703.3M26](/packages/paragonie-random-lib)[delight-im/random

The most convenient way to securely generate anything random in PHP

2345.4k](/packages/delight-im-random)[mistic100/randomcolor

Generate attractive random colors

2431.4M6](/packages/mistic100-randomcolor)[pragmarx/random

Create random chars, numbers, strings

714.2M5](/packages/pragmarx-random)[nubs/random-name-generator

A library to create interesting, sometimes entertaining, random names.

135680.2k3](/packages/nubs-random-name-generator)

PHPackages © 2026

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