Skip to content

Small library for distributing uniformly-generated uint32_t values (such as from an RNG) over a wide variety of types, shapes, and ranges.

License

Notifications You must be signed in to change notification settings

andrewwillmott/distribute-lib

Repository files navigation

DistributeLib

A small library that contains:

  • A number of 32-bit sequence generators -- various classic RNGs, plus a set of stratified progressive sequences.

  • Functions for converting 32-bit integer inputs from such generators into various 1D, 2D, and 3D distributions and types. The intended use is to convert the raw output of any generator into various useful distributions.

Features:

  • Generation of int, float, 2D and 3D samples over various shapes
  • Cheap routines that only take one input number, but may be less accurate or controllable
  • More accurate routines that take a full number of input samples, e.g., three for a cube
  • Pre-modulation of inputs into triangular, gaussian-like, and weighted forms
  • Gaussian (normal) distribution as both a cheap approximation and via full Box-Muller transform
  • Generators supplying LCG/PCG/XorShift/Hash/Halton/Sobol/Golden/'R' sequences.

To build and run the test app:

make
./distribute 1000  # also see distribute.svg after running

Or add Generate.* and Distribute.* to your favourite IDE.

There is an online demo of most of the included generators, modifiers, and distribution functions.

Examples

Assuming 'rng' returns the next sample via operator uint32_t(), usage can be as simple as that below. Otherwise, substitute MyRNG() or whatever other syntax is appropriate for your sample generator.

1D:

float score     = ToFloat(rng, 1.0f, 100.0f);
int   modifier  = ToInt32Signed(rng, 5);
int   dayOfYear = ToInt32Inclusive(rng, 1, 365);
float weightKG  = ToFloat(ModGaussLike(rng), 50.0f, 130.0f);

2D/3D:

Vec2f discLoc       = ToDisc(generator);
Vec2f pixTentSample = ToSquare(ModTriangle(rng), ModTriangle(rng));
Vec3f rayDir        = ToDirection3(rng);

Warning: do not use rand() to feed these functions, as, in addition to its low quality, its range is not guaranteed to be the full 32 bits.

Output

  • 1D: float [-1, 1], integer [-10, 10], with a simple LCG input.

  • 2D: square, ring, triangle

  • Triangle Modifier: integer, float, square in both dimensions

  • Gauss-like Modifier: integer, square, and full gaussian for comparison.

  • Weighted Modifier: float and circle weighted by [1, 8, 0, 4, 1]

  • Non-random inputs: Halton 100, 1000, 5000 samples

  • Non-random inputs: Golden ratio square, circle, and sphere surface

  • Progressively adding samples from the PCG, Halton, and Golden sequences

    PCG Circle Halton Circle Golden (Spiral) Circle

  • Progressively adding samples from the Rd sequence: square, circle, cube

    Rd Square Rd Circle Rd Cube

About

Small library for distributing uniformly-generated uint32_t values (such as from an RNG) over a wide variety of types, shapes, and ranges.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published