Skip to content

CreateRandom

Narta Xaymar Dirks edited this page Dec 2, 2026 · 1 revision

CreateRandom BlitzLLVM

CreateRandom:Int32 engine:Int32=0, seed:Int32=0
Parameter Mode Type Default Description
engine In Int32 0 How the numbers are made, see the table. Defaults to 0 (PCG32).
seed In Int32 0 Where its numbers start. 0 or left out takes one from the clock.

Returns: The handle of the new generator.

Description

BlitzLLVM This command does not exist in the original Blitz products. BlitzLLVM adds it.

Makes a random generator of its own, which gives numbers with SampleRandom. This is a BlitzLLVM extension: the originals have only the one generator of Rnd and Rand, so a particle effect that calls Rnd changes what the next loot roll gets, and a world can't be made again from its seed.

  • Every generator has its own state. Drawing from one never moves another one, or the generator of Rnd and Rand.
  • The same seed gives the same numbers on every system and with every build, so a seed makes the same map on Windows, Linux and macOS.
  • The engine says how the numbers are made. The constants are in Include "<random.bb>", see Include-Files:
Engine Constant State What it is for
0 BB_RANDOM_DEFAULT The same as 1.
1 BB_RANDOM_PCG32 64 bits PCG32: fast and good for games. Repeats after 2^64 numbers.
2 BB_RANDOM_XOSHIRO 128 bits xoshiro128**: repeats after 2^128 - 1 numbers.
3 BB_RANDOM_BLITZ 31 bits The generator of Rnd and Rand: for the same seed, SampleRandom gives what Rand and Rnd give, with their 16 bits. For old programs that need their numbers to stay as they were, but in a stream of their own.
4 BB_RANDOM_MT19937 19937 bits Mersenne Twister: equidistributed in up to 623 dimensions, for simulations and statistics. Gives the numbers of std::mt19937 of C++ for the same seed. A bit slower, and its state is 5000 digits long.
  • A seed of 0, or none, takes a seed from the clock, so the numbers differ on every run. Two generators made at once still get different seeds.
  • RandomState and SetRandomState save and restore where a generator is, for save games and network snapshots.
  • FreeRandom frees it when it is no longer needed.

Differences

  • Not in the originals, which have one generator for the whole program.

Examples

world = CreateRandom(0, 1234) ; the map, the same for every player who has the seed
effects = CreateRandom()      ; particles and sounds, different every run
For i = 1 To 3
	Print SampleRandom(world, 1, 100)
	spark# = SampleRandom(effects)  ; doesn't change the next number of world
Next
FreeRandom world
FreeRandom effects
62
5
28

Related Commands

FreeRandom BlitzLLVM, RandomState BlitzLLVM, SampleRandom BlitzLLVM, SeedRandom BlitzLLVM, SetRandomState BlitzLLVM

Home · Command Index


Getting started

Language

Tools


Language Features
BlitzBasic
Blitz3D
BlitzPlus
Differences

More

Clone this wiki locally