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

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

paragonie/random-lib
====================

A Library For Generating Secure Random Numbers

v2.0.3(2y ago)703.3M—0.6%820MITPHPPHP &gt;=5.3.2

Since Jul 30Pushed 2y ago3 watchersCompare

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

READMEChangelog (4)Dependencies (6)Versions (11)Used By (20)

RandomLib
=========

[](#randomlib)

[![Build Status](https://camo.githubusercontent.com/1503f351767a8a8f06225bfa1d445f43f9599a113de02b91caea6ea2d92acc0c/68747470733a2f2f7472617669732d63692e6f72672f70617261676f6e69652f52616e646f6d4c69622e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/paragonie/RandomLib)[![Latest Stable Version](https://camo.githubusercontent.com/037d058666e8c9706931e5a7618287899deb8a127d7bf96eaf2d14a0882bc9bd/68747470733a2f2f706f7365722e707567782e6f72672f70617261676f6e69652f72616e646f6d2d6c69622f762f737461626c65)](https://packagist.org/packages/paragonie/random-lib)[![Latest Unstable Version](https://camo.githubusercontent.com/6e15df01c6cc3de012693a65c66959560c80f1ce019f8cd35d5dea626620359d/68747470733a2f2f706f7365722e707567782e6f72672f70617261676f6e69652f72616e646f6d2d6c69622f762f756e737461626c65)](https://packagist.org/packages/paragonie/random-lib)[![License](https://camo.githubusercontent.com/c4230e85516d4cec46e8a182b78388df2fc74a39df0f9ff177b92c14b67785a1/68747470733a2f2f706f7365722e707567782e6f72672f70617261676f6e69652f72616e646f6d2d6c69622f6c6963656e7365)](https://packagist.org/packages/paragonie/random-lib)

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

This library is useful in security contexts.

> Note: This is a fork of [Anthony Ferrara's `RandomLib`](https://github.com/ircmaxell/RandomLib), maintained by [Paragon Initiative Enterprises](https://paragonie.com).

Install
-------

[](#install)

Via Composer

```
$ composer require paragonie/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.

### 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 **\#phpc**channel on **irc.freenode.net**.

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

[](#security-vulnerabilities)

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

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity55

Moderate usage in the ecosystem

Community35

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~559 days

Recently: every ~695 days

Total

8

Last Release

758d ago

Major Versions

1.0.x-dev → v2.0.0-rc12017-10-05

### Community

Maintainers

![](https://www.gravatar.com/avatar/05d241256cda885139a5697d3bb536b5cec3b430c1adb9c524bf92a37a55758d?d=identicon)[paragonie-scott](/maintainers/paragonie-scott)

---

Top Contributors

[![ircmaxell](https://avatars.githubusercontent.com/u/660654?v=4)](https://github.com/ircmaxell "ircmaxell (36 commits)")[![paragonie-security](https://avatars.githubusercontent.com/u/15914520?v=4)](https://github.com/paragonie-security "paragonie-security (23 commits)")[![simensen](https://avatars.githubusercontent.com/u/191200?v=4)](https://github.com/simensen "simensen (11 commits)")[![flip111](https://avatars.githubusercontent.com/u/2244480?v=4)](https://github.com/flip111 "flip111 (4 commits)")[![ramsey](https://avatars.githubusercontent.com/u/42941?v=4)](https://github.com/ramsey "ramsey (4 commits)")[![lavoiesl](https://avatars.githubusercontent.com/u/1216046?v=4)](https://github.com/lavoiesl "lavoiesl (3 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)")[![gajus](https://avatars.githubusercontent.com/u/973543?v=4)](https://github.com/gajus "gajus (1 commits)")[![garak](https://avatars.githubusercontent.com/u/179866?v=4)](https://github.com/garak "garak (1 commits)")[![abacaphiliac](https://avatars.githubusercontent.com/u/1656273?v=4)](https://github.com/abacaphiliac "abacaphiliac (1 commits)")[![LukasReschke](https://avatars.githubusercontent.com/u/878997?v=4)](https://github.com/LukasReschke "LukasReschke (1 commits)")[![Mabahe](https://avatars.githubusercontent.com/u/1684986?v=4)](https://github.com/Mabahe "Mabahe (1 commits)")[![Maks3w](https://avatars.githubusercontent.com/u/1301698?v=4)](https://github.com/Maks3w "Maks3w (1 commits)")[![paragonie-scott](https://avatars.githubusercontent.com/u/11591518?v=4)](https://github.com/paragonie-scott "paragonie-scott (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)")[![hansott](https://avatars.githubusercontent.com/u/3886384?v=4)](https://github.com/hansott "hansott (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)")

---

Tags

randomcryptographyrandom-numbersrandom-strings

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[ircmaxell/random-lib

A Library For Generating Secure Random Numbers

84130.2M119](/packages/ircmaxell-random-lib)[paragonie/random_compat

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

8.2k655.0M405](/packages/paragonie-random-compat)[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)
