Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/g105b/drng

Deterministic random number generator.
https://github.com/g105b/drng

Last synced: about 1 month ago
JSON representation

Deterministic random number generator.

Awesome Lists containing this project

README

        

Deterministic random number generator
=====================================

This library generates byte sequences with the ability to convert to integers and floating points. The sequences generated appear to be random, but can be fully determined by providing a known seed value. The main use of this library is in procedural generation algorithms.

The cryptographic algorithms provided by OpenSSL are used to generate the random data, specifically using the Advanced Encryption Standard (AES) operating in Galois/Counter mode (GCM).

Each instance of the `Random` class maintains its AES counter, so there is theoretically no limit to the amount of random data produced. The deterministic nature of this library is possible due to the counter's persistence while generating the initialisation vector, along with the encryption key being set from a provided seed value.

*****

Installation
------------

Composer can be used to install this library into your project. `composer require g105b/drng` will install the latest version. Alternatively, you can modify your project's `composer.json` manually:

```json
{
"require": {
"g105b/drng": "1.*"
}
}
```

Usage example
-------------

### Using a string seed to determine the random sequence

`string-seed.php`:

```php
use g105b\drng\Random;
use g105b\drng\StringSeed;

$rand = new Random(
new StringSeed("i like cats")
);

echo "Random sequence: ";

for($i = 0; $i < 10; $i++) {
if($i > 0) {
echo ", ";
}

echo $rand->getInt(1, 10);
}
```

The above example will always output the same sequence, due to the use of the seed "i like cats":

```
Random sequence: 1, 9, 7, 6, 5, 6, 8, 10, 2, 5
```