Skip to content
FutureBlitz

    Install

    Import std.math.random

    std.math.random

    Random: a seeded generator of random numbers, PCG32, Melissa O'Neill's permuted congruential generator, so a seed gives the same numbers in every build, on every OS and CPU, which replays, tests, and level seeds need. Its floats come from its integers, and Normal's logarithm is its own; of C's libm, only Normal, UnitVector3, and Normalize use sqrt, which IEEE 754 has every OS round correctly. It isn't for secrets; std.crypto's SecureRandom is.

    std/math/random.fb

    Types

    Random class

    Public Class Random

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

    std/math/random.fb, line 215

    • 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.

    Functions

    SeedRandom function

    Public Function SeedRandom(seed: Int64)

    Starts the default generator on every thread again from the seed, so it gives the numbers Random(seed) does, on each thread, from that thread's next draw: the same numbers on each run.

    std/math/random.fb, line 378

    Rand function

    Public Function Rand(): Float

    A Float from 0 up to 1, 1 left out, from this thread's default generator.

    std/math/random.fb, line 387

    RandInt function

    @PanicsAtCaller Public Function RandInt(count: Int): Int

    An Int from 0 to count - 1, each as likely, from this thread's default generator. A count that isn't above 0 panics.

    std/math/random.fb, line 394

    RandInt function

    @PanicsAtCaller Public Function RandInt(low: Int, high: Int): Int

    An Int from low to high, both included, each as likely, from this thread's default generator: RandInt(1, 6) rolls a die. A low above high panics.

    std/math/random.fb, line 403

    RandFloat function

    @PanicsAtCaller Public Function RandFloat(low: Float, high: Float): Float

    A Float from low up to high, high left out, from this thread's default generator: low itself when they're equal. A low above high, or a NaN or infinite end, panics.

    std/math/random.fb, line 414

    Extends

    T[] extend

    Extend T[]

    No doc comment.

    std/math/random.fb, line 423

    • Public Method Shuffle() method #

      Puts the elements in a random order, in place, each order as likely, from this thread's default generator.

    • Public Method Shuffle(random: Random) method #

      Puts the elements in a random order, in place, each order as likely, from the generator.

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

      count of the elements, none twice, in a random order, as a new array, from this thread's default generator. A count below 0 or above the elements' panics.

    • @PanicsAtCaller Public Method Sample(count: Int, random: Random): T[] method #

      count of the elements, none twice, in a random order, as a new array, from the generator. A count below 0 or above the elements' panics.

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