IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.0.0b1
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

random

Provides functions for pseudorandom numbers.

You can import these APIs from the random package. For example:

from std.random import seed

These functions use a shared, global pseudorandom number generator (PRNG) state. The global random state is shared across threads and concurrent access can cause race conditions and undefined behavior.

Warning: NOT cryptographically secure. This PRNG is suitable for simulations, games, and general statistical purposes, but shouldn't be used for security-sensitive applications such as generating passwords, authentication tokens, or encryption keys.

Functions

  • rand: Fills memory with random values from a uniform distribution.
  • randint: Fills memory with uniformly distributed random integers in the range [low, high].
  • randn: Fills memory with random values from a Normal distribution.
  • randn_float64: Returns a random Float64 sampled from a normal distribution.
  • random_float64: Returns a random Float64 number from the given range [min, max).
  • random_si64: Returns a random Int64 number from the given range [min, max].
  • random_ui64: Returns a random UInt64 number from the given range [min, max].
  • seed: Seeds the random number generator using a time-based value.
  • shuffle: Shuffles the elements of the list randomly.