τau /

math/rand

module
rand = import("math/rand")

rand - pseudo random numbers.

New returns a source seeded with a number, so a program that wants the same sequence every time can have it. The functions of the module itself use one source seeded from the clock at startup:

rand = import("math/rand")
rand.Intn(6) + 1

The generator is xorshift64*, which is small and good enough for shuffles, sampling and tests. It is not for keys or tokens: Crypto reads those from the system.

New = fn(seed)source

New returns a source of random numbers seeded with seed. Two sources with the same seed give the same sequence.

Int = fn()source

Int returns a number between 0 and 2^63 - 1.

Intn = fn(n)source

Intn returns a number between 0 and n - 1. n must be positive.

Float = fn()source

Float returns a number between 0 (included) and 1 (excluded), with the 53 bits a float can hold.

Bool = fn()source

Bool returns true about half the time.

Bytes = fn(n)source

Bytes returns n random bytes.

Perm = fn(n)source

Perm returns the numbers from 0 to n - 1 in random order.

Shuffle = fn(xs)source

Shuffle returns the elements of xs in random order, leaving xs alone.

Choice = fn(xs)source

Choice returns one element of xs, or null when there are none.

Seed = fn(seed)source

Seed restarts the sequence of the module from seed, for a program that wants the same numbers on every run. The state is written into the source that is already there, since a function assigning to global would only be writing to a name of its own.

Int = fn()source

Int returns a number between 0 and 2^63 - 1.

Intn = fn(n)source

Intn returns a number between 0 and n - 1. n must be positive.

Float = fn()source

Float returns a number between 0 (included) and 1 (excluded), with the 53 bits a float can hold.

Bool = fn()source

Bool returns true about half the time.

Bytes = fn(n)source

Bytes returns n random bytes.

Perm = fn(n)source

Perm returns the numbers from 0 to n - 1 in random order.

Shuffle = fn(xs)source

Shuffle returns the elements of xs in random order, leaving xs alone.

Choice = fn(xs)source

Choice returns one element of xs, or null when there are none.

Int = fn()source

Intn = fn(n)source

Float = fn()source

Bool = fn()source

Bytes = fn(n)source

Perm = fn(n)source

Shuffle = fn(xs)source

Choice = fn(xs)source

Crypto = fn(n)source

Crypto returns n bytes from the system generator, the ones to use for keys, tokens and passwords. Unlike the rest of the module it can fail, when /dev/urandom isn't there.

ponytail: /dev/urandom rather than getrandom(2), so no C is needed. On Windows it fails, which is where a sys_getrandom would have to go.