miso
Copyright(C) 2016-2026 David M. Johnson
LicenseBSD3-style (see the file LICENSE)
MaintainerDavid M. Johnson <code@dmj.io>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

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 StdGen through your code using next (analogous to System.Random).
  • Global generator — use replicateRM or access globalStdGen directly for fire-and-forget random values.

Quick start

import Miso.Random

-- Explicit generator
example :: IO ()
example = do
  gen         <- newStdGen
  let (v, g') = next gen     -- v :: Double in [0, 1)
  print v

-- Global generator (convenience)
tenValues :: IO [Double]
tenValues = replicateRM 10

-- Reproducible seed for tests
deterministicGen :: StdGen
deterministicGen = mkStdGen 42

Seeding

  • newStdGen seeds from crypto.getRandomValues() — cryptographically random, non-reproducible.
  • mkStdGen takes an explicit Seed (Int) — reproducible, useful for property tests or simulations.
  • globalStdGen is seeded once at module load time from Math.random().

See also

Synopsis

Types

newtype StdGen Source #

StdGen holds a JS Function.

Constructors

StdGen Function 

type Seed = Int Source #

An initial Seed value, useful for simulations or reproducing test failures

Functions

newStdGen :: IO StdGen Source #

Create a new StdGen, defaulting to a random Seed.

mkStdGen Source #

Arguments

:: Seed

Initial seed value; identical seeds produce identical sequences

-> StdGen 

Like newStdGen but takes a Seed as an argument and is pure.

next Source #

Arguments

:: StdGen

Current generator state

-> (Double, StdGen) 

Get the next StdGen, extracting the value, useful with State.

replicateRM Source #

Arguments

:: Int

Number of random Double values to generate in [0, 1)

-> IO [Double] 

Generate n amount of random numbers. Uses the global PRNG globalStdGen.

replicateRM 10 :: IO [Double]

setStdGen Source #

Arguments

:: StdGen

New generator to install as the global PRNG

-> IO () 

Set the globalStdGen

Globals

globalStdGen :: IORef StdGen Source #

Global StdGen, used by replicateRM and others.