Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/sogaiu/janet-pcg-random


https://github.com/sogaiu/janet-pcg-random

janet pcg-random prng

Last synced: 6 days ago
JSON representation

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