# paragonie/random_compat

**Attribution required: if you use, quote, or summarise this content, you must credit and link back to [awesome-repositories.com](https://awesome-repositories.com/repository/paragonie-random-compat).**

8,165 stars · 148 forks · PHP · MIT

## Links

- GitHub: https://github.com/paragonie/random_compat
- Homepage: https://paragonie.com/projects
- awesome-repositories: https://awesome-repositories.com/repository/paragonie-random-compat.md

## Topics

`csprng` `php` `php5` `polyfill` `random` `random-bytes` `random-generation` `random-int` `randomization` `randomness` `secure` `secure-by-default`

## Description

random_compat is a backward-compatibility library that provides cryptographically secure random number generation functions for PHP 5.x environments. It delivers the `random_bytes()` and `random_int()` functions that are natively available in PHP 7+, enabling legacy applications to generate secure random bytes and unbiased integers from system entropy sources.

The library implements a priority-based fallback chain across operating-system CSPRNGs, selecting the best available random source in order of security. It uses rejection sampling to convert raw random bytes into unbiased integers within arbitrary ranges, eliminating modulo bias. When no secure random source is available, the library throws explicit exceptions to make failure modes clear to developers.

The polyfill defines its functions only when the native PHP 7+ equivalents are absent, preventing redeclaration errors in mixed-version environments. It wraps multiple OS-level random number generators behind a unified interface, hiding platform-specific differences from callers. The library exposes procedural functions that internally delegate to static class methods, maintaining PHP 5.x compatibility without breaking modern call patterns.

## Tags

### Security & Cryptography

- [Cryptographic Random Number Generators](https://awesome-repositories.com/f/security-cryptography/cryptographic-random-number-generators.md) — Generates unbiased random bytes and integers from system entropy sources for security-sensitive operations. ([source](https://github.com/paragonie/random_compat/blob/master/psalm-autoload.php))
- [PHP 5.x Random Function Compatibility](https://awesome-repositories.com/f/security-cryptography/cryptographic-random-number-generators/php-5-x-random-function-compatibility.md) — Provides cryptographically secure random number generation functions for legacy PHP 5.x applications.

### Graphics & Multimedia

- [Cryptographic Integer Scaling](https://awesome-repositories.com/f/graphics-multimedia/audio-music/audio-processing/integer/cryptographic-integer-scaling.md) — Converts raw random bytes into unbiased integers within arbitrary ranges using rejection sampling.

### Networking & Communication

- [Fallback Chains](https://awesome-repositories.com/f/networking-communication/traffic-management-gateways/circuit-breakers/fallback-chains.md) — Selects the best available random source by trying OS-level CSPRNGs in order of security.
- [Priority-Based CSPRNG](https://awesome-repositories.com/f/networking-communication/traffic-management-gateways/circuit-breakers/fallback-chains/priority-based-csprng.md) — Selects the best available operating-system random source through a security-prioritized fallback chain.

### Programming Languages & Runtimes

- [Unbiased Random Integer Generators](https://awesome-repositories.com/f/programming-languages-runtimes/language-features-paradigms/language-features/core-conceptual-frameworks/programming-language-concepts/random-number-generation/random-number-generators/unbiased-random-integer-generators.md) — Produces unbiased random integers within arbitrary ranges using rejection sampling to avoid modulo bias.

### Software Engineering & Architecture

- [Cross-Platform Abstractions](https://awesome-repositories.com/f/software-engineering-architecture/cross-platform-abstractions.md) — Wraps multiple OS-level CSPRNGs behind a unified interface, hiding platform-specific differences from callers.

### Development Tools & Productivity

- [Backward Compatibility Libraries](https://awesome-repositories.com/f/development-tools-productivity/backward-compatibility-libraries.md) — Delivers secure random number generation functions to PHP 5.x applications that cannot use PHP 7+ native implementations.
- [PHP 5.x Compatibility Polyfills](https://awesome-repositories.com/f/development-tools-productivity/php-5-x-compatibility-polyfills.md) — Provides cryptographically secure random byte and integer generation functions for PHP 5.x environments.
- [PHP 5.x CSPRNG Polyfills](https://awesome-repositories.com/f/development-tools-productivity/php-5-x-csprng-polyfills.md) — Provides cryptographically secure random_bytes() and random_int() functions for legacy PHP 5.x environments.
