https://github.com/semibran/prnguin
:penguin: deterministic pseudorandom number generation
https://github.com/semibran/prnguin
math prng probability random rng
Last synced: about 1 year ago
JSON representation
:penguin: deterministic pseudorandom number generation
- Host: GitHub
- URL: https://github.com/semibran/prnguin
- Owner: semibran
- License: mit
- Created: 2017-01-27T00:14:25.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-04-25T17:12:00.000Z (about 8 years ago)
- Last Synced: 2025-03-16T20:35:31.212Z (over 1 year ago)
- Topics: math, prng, probability, random, rng
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/prnguin
- Size: 19.5 KB
- Stars: 12
- Watchers: 1
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# prnguin
This module contains a small collection of simple "generators", i.e. pure functions that transform a number into a predictable output value. You will usually want to change the seed after every use (typically by incrementing it) unless your goal is to get the same results multiple times.
Since generators are just functions, crafting your own custom generators to match specific scenarios is a trivial matter. For example, the following generator simulates the rolling of a six-sided die.
```js
function roll(seed) {
return integer(1, 6, seed)
}
```
Additionally, all the functions in this module derive their seemingly random nature from `percent`, further demonstrating their composability.
## usage
[](https://www.npmjs.com/package/prnguin)
```js
const { percent, integer, boolean, pick, shuffle } = require("prnguin")
```
### `percent(seed)`
Generate a number between `0` and `1` using `seed`, **exclusively**.
```js
if (percent(seed++) < 0.1) {
money.type = "gold"
} else {
money.type = "copper"
}
```
`1` **is not** included in the set of possible return values, so this function is essentially a seedable version of `Math.random()`.
### `integer(min, max, seed)`
Generate an integer between `min` and `max` using `seed`, **inclusively**.
```js
> integer(1, 10, seed++)
6
```
Unlike `percent`, the upper number (`max`) is included in the set of possible return values.
### `boolean(seed)`
Choose either `true` and `false` based on the given `seed`.
```js
if (boolean(seed++)) {
for (let connector of connectors) {
floors.push(connector)
}
} else {
let connector = pick(connectors, seed++)
doors.push(connector)
}
```
### `pick(array, seed)`
Chooses a random element from `array` using `seed`.
```js
let card = pick(deck, seed++)
```
### `shuffle(array, seed)`
Shuffles the items in `array` in place using `seed`.
```js
shuffle(deck, seed++)
```
## license
[MIT](https://opensource.org/licenses/MIT) © [Brandon Semilla](https://git.io/semibran)