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.
-
A generator from the
seed, on PCG's default stream. -
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 negativestreampanics, since PCG takes thestream's low 63 bits, so it would give a stream from 0 up's numbers. -
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.
-
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.
-
Any Int, each as likely.
-
An Int from 0 to
count- 1, each as likely, as for an index. A count that isn't above 0 panics. -
An Int from
lowtohigh, both included, each as likely: Int(1, 6) rolls a die. A low abovehighpanics. -
A Float from 0 up to 1, 1 left out, from 24 random bits, so each of its values is as likely.
-
A Float from
lowup tohigh,highleft out:lowitself when they're equal. A low abovehigh, or a NaN or infinite end, panics. The span is worked out in Doubles, so ends a Float's whole range apart are fine. -
A Double from 0 up to 1, 1 left out, from 53 random bits.
-
True or False, each as likely.
-
True with the
probability, from 0 for never to 1 for always. -
One of the
items, each as likely. No items panics. -
One of the
items, each as likely as its weight is of theweights' sum. Weights anditemsof other counts, a weight that's negative or not finite, orweightsthat add up to 0, panic. Weights whose sum overflows are taken as shares of the largest. -
Puts the
itemsin a random order, in place, each order as likely: Fisher and Yates' shuffle. -
countof theitems, none twice, in a random order, as a new array, each choice as likely. Acountbelow 0 or above theitems' panics. -
A number from the normal distribution, the bell curve, of the
meanand standarddeviation: Marsaglia's polar method, with a logarithm of its own, so it's the same everywhere. -
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.
-
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.
Rand
function
Public Function Rand(): Float
A Float from 0 up to 1, 1 left out, from this thread's default generator.
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.
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.
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.
Extends
T[]
extend
Extend T[]
No doc comment.
-
Puts the elements in a random order, in place, each order as likely, from this thread's default generator.
-
Puts the elements in a random order, in place, each order as likely, from the generator.
-
countof 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. -
countof the elements, none twice, in a random order, as a new array, from the generator. Acountbelow 0 or above the elements' panics.
Report an issue with this page or with FutureBlitz on GitHub.