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

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

pragmarx/random
===============

Create random chars, numbers, strings

v0.2.2(8y ago)714.2M—9%3[1 issues](https://github.com/antonioribeiro/random/issues)5MITPHPPHP &gt;=7.0

Since Sep 18Pushed 8y ago2 watchersCompare

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

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

Random
======

[](#random)

[![Latest Stable Version](https://camo.githubusercontent.com/3ed178eaddccc5ed58661b7a7d9c33640a9eb378babd1c292a4b0534f8b9b81c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f707261676d6172782f72616e646f6d2e7376673f7374796c653d666c61742d737175617265267570646174653d313233)](https://packagist.org/packages/pragmarx/random)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/d8ff66599ca29d55c334b6ddb9af826d72c9d5916fcdf1d3c1ab0375fcd386dd/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f6275696c642f672f616e746f6e696f7269626569726f2f72616e646f6d2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/antonioribeiro/random/build-status/master)[![Code Coverage](https://camo.githubusercontent.com/c2a253ba3e3da588bd0faaf2d610f29e0a5b932acc8e847f4a8760515d3fe4b4/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f616e746f6e696f7269626569726f2f72616e646f6d2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/antonioribeiro/random/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/4a9355e4e239fe045e734f318672cb2ff94b78d747d4ed057d0d3537a228139d/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f616e746f6e696f7269626569726f2f72616e646f6d2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/antonioribeiro/random/?branch=master)[![StyleCI](https://camo.githubusercontent.com/0d3537ac5babfe04c5bf410c0877b36701cc8fca8c74b04f0ac4f56f3b5d1ef9/68747470733a2f2f7374796c6563692e696f2f7265706f732f3130333536383231392f736869656c64)](https://styleci.io/repos/103996703)

#### Generate random strings, numbers, bytes, patterns, and a lot more

[](#generate-random-strings-numbers-bytes-patterns-and-a-lot-more)

Features
--------

[](#features)

It generates cryptographically secure **pseudo-random** bytes (using `random_bytes()` and `random_int()`) to make:

- Strings
- Numbers (strings or integers)
- Upper, lower and mixed case
- Prefixed and suffixed random strings
- Hexadecimal
- Regex patterns (\[abcd\], \[aeiou\], \[A-Z0123\], \[0-9a-f\])
- Raw strings, giving you whatever `random_bytes()` generates

#### Faker

[](#faker)

If you have [Faker](https://github.com/fzaninotto/Faker) installed it falls back to it, giving you access to random names, dates, cities, phones, and a lot more.

Install
-------

[](#install)

Via Composer

```
$ composer require pragmarx/random
```

Usage
-----

[](#usage)

#### Basic array usage

[](#basic-array-usage)

```
$this->random = new PragmaRX\Random\Random();

$this->random->get(); /// will generate an alpha string which is the same of

$this->random->alpha()->get();
```

Should give you 16 chars (default size) string

```
   Ajv3ejknLmqwC36z

```

#### Defining the size

[](#defining-the-size)

```
$this->random->size(32)->get();
```

#### Upper and lower case

[](#upper-and-lower-case)

```
$this->random->uppercase()->get();
$this->random->lowercase()->size(255)->get();
```

To get back to mixed case you can just:

```
$this->random->mixedcase()->get();
```

#### Defining a pattern

[](#defining-a-pattern)

The pattern method uses regex, so you can:

```
$this->random->pattern('[abcd]')->get();

$this->random->pattern('[A-F0-9]')->get(); /// Hexadecimal
```

To get

```
   abcddcbabbacbbdabbcb

```

#### Numeric and Integer

[](#numeric-and-integer)

The pattern method uses regex, so you can:

```
$this->random->numeric()->start(10)->end(20)->get();
```

To get

```
   (int) 18

```

But if you set the size

```
$this->random->numeric()->size(3)->get();
```

You'll get a string

```
   (string) 123

```

#### Hexadecimal

[](#hexadecimal)

```
$this->random->hex()->size(10)->get();
```

Hexadecimal is uppercase by default, but you can get a lowercase by doing:

```
$this->random->hex()->lowercase()->get();
```

#### Prefix &amp;&amp; Suffix

[](#prefix--suffix)

```
$this->random->hex()->prefix('#')->size(6)->lowercase()->get();
```

And you should get a random CSS color:

```
#fafafa

```

Of course, the same works for suffixes

```
$this->random->prefix('!')->suffix('@')->get();
```

#### Trivia

[](#trivia)

There are currently 43982 questions in the trivia database, and this is how you get them:

```
$this->random->trivia()->get();

$this->random->trivia()->count(2)->get();
```

You'll need to install the Trivia database package:

```
$ composer require pragmarx/trivia
```

#### Faker

[](#faker-1)

If you install Faker

```
composer require fzaninotto/faker
```

You'll also have access to all of the Faker features, like:

```
$this->random->city()->get();
```

And also use all other features of Random

```
$this->random->prefix('city: ')->city()->lowercase()->get();
```

You can also change the faker class, you another one pleases you more:

```
$this->random->fakerClass(AnotherFaker\Factory::class)->date()->get();
```

#### Raw strings

[](#raw-strings)

Usually the package returns characters in the range of Base64 (A to Z, a to z and 0 to 9), but you can completely disable this feature and make it return whatever `random_bytes()` generates:

```
$this->random->raw()->get();
```

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
$ composer update
$ vendor/bin/phpunit
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Antonio Carlos Ribeiro](https://github.com/antonioribeiro)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity55

Moderate usage in the ecosystem

Community19

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 95.3% 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 ~16 days

Total

5

Last Release

3101d ago

### Community

Maintainers

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

---

Top Contributors

[![antonioribeiro](https://avatars.githubusercontent.com/u/3182864?v=4)](https://github.com/antonioribeiro "antonioribeiro (41 commits)")[![carusogabriel](https://avatars.githubusercontent.com/u/16328050?v=4)](https://github.com/carusogabriel "carusogabriel (1 commits)")[![jerguslejko](https://avatars.githubusercontent.com/u/4470539?v=4)](https://github.com/jerguslejko "jerguslejko (1 commits)")

---

Tags

fakernumberspatternsphprandomstringsrandomfakerpragmarxrandom stringRandomizerandom numberrandom pattern

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[paragonie/random_compat

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

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

Faker is a PHP library that generates fake data for you.

4.0k358.5M3.5k](/packages/fakerphp-faker)[ircmaxell/random-lib

A Library For Generating Secure Random Numbers

84130.2M119](/packages/ircmaxell-random-lib)[mbezhanov/faker-provider-collection

A collection of custom providers for the Faker library

2138.6M24](/packages/mbezhanov-faker-provider-collection)[mistic100/randomcolor

Generate attractive random colors

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

Laravel Illuminate collection with objectified properties

943.4M11](/packages/pragmarx-coollection)

PHPackages © 2026

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