| Copyright | (C) 2016-2026 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Miso.Random
Description
Overview
Miso.Random provides a pseudo-random number generator for miso
components and their test infrastructure. It is built on the
SplitMix32 algorithm, implemented as a
stateful JavaScript function stored in a Function.
Two usage styles are available:
- Explicit generator — pass a
StdGenthrough your code usingnext(analogous toSystem.Random). - Global generator — use
replicateRMor accessglobalStdGendirectly for fire-and-forget random values.
Quick start
import Miso.Random -- Explicit generator example :: IO () example = do gen <-newStdGenlet (v, g') =nextgen -- v :: Double in [0, 1) print v -- Global generator (convenience) tenValues :: IO [Double] tenValues =replicateRM10 -- Reproducible seed for tests deterministicGen ::StdGendeterministicGen =mkStdGen42
Seeding
newStdGenseeds fromcrypto.getRandomValues()— cryptographically random, non-reproducible.mkStdGentakes an explicitSeed(Int) — reproducible, useful for property tests or simulations.globalStdGenis seeded once at module load time fromMath.random().
See also
splitmix32— the raw JS PRNG primitivegetRandomValue—crypto.getRandomValues()used for seeding
Types
Functions
Get the next StdGen, extracting the value, useful with State.
Generate n amount of random numbers. Uses the global PRNG globalStdGen.
replicateRM 10 :: IO [Double]
Set the globalStdGen
Globals
globalStdGen :: IORef StdGen Source #
Global StdGen, used by replicateRM and others.