id: random.core title: Random.Core
Random numbers are values generated in such a way that they appear unpredictable and evenly distributed within a given range. They are widely used in games, simulations, procedural generation, testing, and anywhere variation or chance is required.
On computers, random numbers are usually pseudo-random rather than truly random. This means they are produced by deterministic algorithms called random number generators (RNGs). Given the same starting state (called a seed), a pseudo-random generator will always produce the same sequence of values. This property is extremely useful for debugging, testing, and reproducing procedural content.
BlitzMax provides a flexible random number system built around the Random.Core module. At its core is the abstract base class TRandom, which defines a common interface for all random number generators.
Concrete implementations of TRandom are provided by separate modules, such as BRL.Random, which contains the original BlitzMax random number generator. Other modules may provide alternative generators with different statistical properties or performance characteristics.
BlitzMax supports two complementary ways of working with random numbers:
When you import a random module, it typically registers a generator and makes it available globally. If multiple random modules are imported, the last imported module becomes the default global generator.
The global generator is used by the standard random functions:
This allows most programs to use random numbers without needing to manage generator objects explicitly.
The active global generator can be queried or changed at runtime using:
If a generator name cannot be found, the currently active generator remains unchanged.
In addition to the global generator, you can create independent instances of random number generators using CreateRandom().
This is useful when:
Instances can be created with or without an explicit seed, and optionally by specifying the generator name. If no name is given, the currently active global generator type is used.
Seeding controls the initial state of a random number generator.
Using the same seed will always produce the same sequence of random values, making it possible to reproduce results exactly. This is especially important for procedural generation, demos, and debugging.
| Type | Description |
|---|---|
| TRandom | Random number generator |
Function SetRandom(name:String)Sets the current random number generator to name.
If no generator called name is found, the current random number generator remains active.
Function GetRandomName:String()Gets the name of the current random number generator.
The name of the current random number generator, or Null if none is set.
Function GetRandomNames:String[]()Gets the names of available random number generators.
Function CreateRandom:TRandom(name:String = Null)Creates a new TRandom instance.
Function CreateRandom:TRandom(seed:Int, name:String = Null)Creates a new TRandom instance with the given seed.
Function RndFloat:Float()Generate random float
A random float in the range 0 (inclusive) to 1 (exclusive)
Function RndDouble:Double()Generate random double
A random double in the range 0 (inclusive) to 1 (exclusive)
Function Rnd:Double(minValue:Double = 1, maxValue:Double = 0)Generate random double
The optional parameters allow you to use Rnd in 3 ways:
| Format | Result |
| `Rnd()` | Random double in the range 0 (inclusive) to 1 (exclusive) |
| `Rnd(x)` | Random double in the range 0 (inclusive) to n (exclusive) |
| `Rnd(x,y)` | Random double in the range x (inclusive) to y (exclusive) |
A random double in the range min (inclusive) to max (exclusive)
Function Rand:Int(minValue:Int, maxValue:Int = 1)Generate random integer
The optional parameter allows you to use Rand in 2 ways:
| Format | Result |
| `Rand(x)` | Random integer in the range 1 to x (inclusive) |
| `Rand(x,y)` | Random integer in the range x to y (inclusive) |
A random integer in the range min (inclusive) to max (inclusive)
Function SeedRnd(seed:Int)Set random number generator seed
Function RndSeed:Int()Get random number generator seed
Used in conjunction with SeedRnd, RndSeed allows you to reproduce sequences of random numbers.
The current random number generator seed