https://github.com/citrineinformatics/sprandom
A splittable, serializable psuedorandom number generator in Scala.
https://github.com/citrineinformatics/sprandom
Last synced: about 2 months ago
JSON representation
A splittable, serializable psuedorandom number generator in Scala.
- Host: GitHub
- URL: https://github.com/citrineinformatics/sprandom
- Owner: CitrineInformatics
- License: apache-2.0
- Created: 2022-06-01T19:05:10.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-08-24T20:19:41.000Z (almost 3 years ago)
- Last Synced: 2025-01-16T08:19:25.010Z (over 1 year ago)
- Language: Scala
- Homepage:
- Size: 45.9 KB
- Stars: 0
- Watchers: 15
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SpRandom
Splittable, serializable pseudorandom number generation in Scala.
The SpRandom package produces random numbers that are reproducible in a multi-threaded context.
It also allows the random state to be serialized and deserialized.
SpRandom's `Random` class is built on top of the `java.util.SplittableRandom` class.
It exposes the following features:
* A `zip` method that facilitates reproducibility in a multi-threaded context
* Serializability by persisting the underlying seed (SplittableRandom is not serializable)
* Multiple, convenient ways to instantiate a random state
* A variety of convenience methods to generate pseudorandom outcomes
## Usage
A `Random` object can be instantiated from an integer seed, a long seed, or another `Random` object.
```scala
import io.citrine.random.Random
val rng = Random(17L)
val rng2 = rng.split() // an independent stream
val uniformRandom = rng.between(1.5, 2.5) // draw from a uniform distribution between 1.5 and 2.5
val normalRandom = rng.nextGaussian(mean = 5.0, stdDev = 0.1) // draw from a normal with mean 5.0 and standard deviation 0.1
val orderedVector = (1 to 10).toVector
val shuffledVector = rng.shuffle(orderedVector) // random permutation of the range (1 to 10)
```
To get reproducible results in a multi-threaded context, use `.zip`.
Imagine that we have some sequence, `items`, that we want to process in parallel.
Processing is done by the stochastic, reproducible method `heavyStochasticComputation`.
The following call is then reproducible.
```scala
rng.zip(items).par.map { case (thisRng, item) => heavyStochasticComputation(item, thisRng) }
```