https://github.com/sogaiu/janet-pcg-random
https://github.com/sogaiu/janet-pcg-random
janet pcg-random prng
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/sogaiu/janet-pcg-random
- Owner: sogaiu
- Created: 2020-07-16T03:44:40.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2025-04-28T07:41:14.000Z (about 1 year ago)
- Last Synced: 2025-08-31T18:41:41.317Z (10 months ago)
- Topics: janet, pcg-random, prng
- Language: C
- Homepage:
- Size: 196 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# janet-pcg-random
[Janet](https://janet-lang.org/) bindings for the [PCG
Random](https://www.pcg-random.org/) pseudo random number generator.
## Status
Early stage, expect changes :)
## Installation
```
jpm install https://github.com/sogaiu/janet-pcg-random
```
## Usage
Make module available:
```janet
(import pcgrand)
```
`pcgrand/make` creates a seeded generator instance:
```janet
(pcgrand/make (int/u64 "1") (int/u64 "1"))
# =>
```
Use two `int/64` values to seed: the state initializer and sequence
selection constant (stream id).
`pcgrand/random` returns a uniformly distributed 32-bit integer:
```janet
(def rng
(pcgrand/make (int/u64 "1") (int/u64 "1")))
# =>
(pcgrand/random rng)
# =>
-914190447
(:random rng)
# =>
361947764
```
`pcgrand/boundedrand` returns a uniformly distributed integer, i,
where 0 <= i < bound:
```janet
(def rng
(pcgrand/make (int/u64 "1") (int/u64 "1")))
# =>
(pcgrand/boundedrand rng 28)
# =>
9
(:boundedrand rng 9)
# =>
2
```
`pcgrand/srandom` seeds a generator using two `int/u64` values in a
manner similar to `pcgrand/make`:
```janet
(def rng
(pcgrand/make (int/u64 "1") (int/u64 "1")))
# =>
(pcgrand/random rng)
# =>
-914190447
(:random rng)
# =>
361947764
(pcgrand/srandom rng (int/u64 "1") (int/u64 "1"))
# =>
nil
(:random rng)
# =>
-914190447
```
## Acknowledgments
* andrewchambers - discussion, sample code
* bakpakin - janet, sample code
* cellularmitosis - documented sample code
* imneme - pcg-random
* pyrmont - documented sample code