Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/g105b/drng
- Owner: g105b
- Created: 2020-02-01T13:57:49.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-09-03T20:56:41.000Z (3 months ago)
- Last Synced: 2024-10-05T10:26:54.909Z (about 2 months ago)
- Language: PHP
- Homepage:
- Size: 24.4 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
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
```