Import std.math.random
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)-
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.
Report an issue with this page or with FutureBlitz on GitHub.