Skip to content
FutureBlitz

    Install

    Import std.math.random

    Random

    std.math.random

    class

    Public Class Random

    Random numbers from a PCG32 generator, the same ones for the same seed, for simulations, procedural content, and tests, not secrets.

    Examples

    Local a = Random(42)
    Local b = Random(42)
    // The same seed gives the same numbers.
    Expect(a.Int(1, 6) == b.Int(1, 6))
    Local roll = a.Int(1, 6)
    Expect(roll >= 1 && roll <= 6)
    Local deck = [1, 2, 3, 4, 5]
    a.Shuffle(deck)
    Expect(deck.Count == 5 && deck.Contains(3))
    Expect(a.Float() < 1.0)

    std/math/random.fb, line 243

    • Public New(seed: Int64) constructor #

      A generator from the seed, on PCG's default stream.

    • @PanicsAtCaller Public New(seed: Int64, stream: Int64) constructor #

      A generator from the seed, on one of 2^63 streams, from 0 up, which give numbers apart from each other for the same seed. A negative stream panics, since PCG takes the stream's low 63 bits, so it would give a stream from 0 up's numbers.

    • Public Property State: (UInt64, UInt64) property #

      The generator's state and increment, to save and to set back, which then gives the numbers it would have gone on to. An even increment is made odd, as PCG needs.

    • Public Method Fork(): Random method #

      A new generator from this one's next numbers, its own seed and stream, for a part of a program that draws apart from the rest, as each of a world's chunks can.

    • Public Method Int(): Int method #

      Any Int, each as likely.

    • @PanicsAtCaller Public Method Int(count: Int): Int method #

      An Int from 0 to count - 1, each as likely, as for an index. A count that isn't above 0 panics.

    • @PanicsAtCaller Public Method Int(low: Int, high: Int): Int method #

      An Int from low to high, both included, each as likely: Int(1, 6) rolls a die. A low above high panics.

    • Public Method Float(): Float method #

      A Float from 0 up to 1, 1 left out, from 24 random bits, so each of its values is as likely.

    • @PanicsAtCaller Public Method Float(low: Float, high: Float): Float method #

      A Float from low up to high, high left out: low itself when they're equal. A low above high, or a NaN or infinite end, panics. The span is worked out in Doubles, so ends a Float's whole range apart are fine.

    • Public Method Double(): Double method #

      A Double from 0 up to 1, 1 left out, from 53 random bits.

    • Public Method Bool(): Bool method #

      True or False, each as likely.

    • Public Method Chance(probability: Double): Bool method #

      True with the probability, from 0 for never to 1 for always.

    • @PanicsAtCaller Public Method Pick<T>(items: T[]): T method #

      One of the items, each as likely. No items panics.

    • @PanicsAtCaller Public Method WeightedPick<T>(items: T[], weights: Double[]): T method #

      One of the items, each as likely as its weight is of the weights' sum. Weights and items of other counts, a weight that's negative or not finite, or weights that add up to 0, panic. Weights whose sum overflows are taken as shares of the largest.

    • Public Method Shuffle<T>(items: T[]) method #

      Puts the items in a random order, in place, each order as likely: Fisher and Yates' shuffle.

    • @PanicsAtCaller Public Method Sample<T>(items: T[], count: Int): T[] method #

      count of the items, none twice, in a random order, as a new array, each choice as likely. A count below 0 or above the items' panics.

    • Public Method Normal(mean: Double = 0, deviation: Double = 1): Double method #

      A number from the normal distribution, the bell curve, of the mean and standard deviation: Marsaglia's polar method, with a logarithm of its own, so it's the same everywhere.

    • Public Method InsideUnitCircle(): Vec2 method #

      A point inside the circle of radius 1, each as likely: a point in the square, drawn again until it's in the circle, as Floats, which can round one just inside it to its edge.

    • Public Method UnitVector3(): Vec3 method #

      A direction in 3D, of length 1, each as likely: Marsaglia's method, from a point in the circle.

    Report an issue with this page or with FutureBlitz on GitHub.