Blitz3D+ Command Reference

RndSeed ( )

Parameters

None.

Description

Returns the random number generator's current seed value.

Blitz3D+ random numbers come from a fixed sequence driven by a single integer. SeedRnd sets that integer; RndSeed reads it back. Every Rnd or Rand call advances it to the next value in the sequence, so the number you get back is not the seed you last passed in - it is wherever the generator has reached.

That makes RndSeed and SeedRnd a save-and-restore pair. Read the seed, do whatever rolling you like, then SeedRnd it back and the generator carries on exactly as though those rolls never happened. Handy when a map preview, an AI "what if" pass or a replay must not disturb the sequence the real game is using, and equally handy for saving a game: store RndSeed() in the save file and the loaded game continues the same run of numbers.

It is also the cheapest way to record a procedurally generated level. Seed from a number, note it with RndSeed, and that one integer regenerates the whole dungeon later - a level "code" a player can share.

A fresh program always starts from the same built-in seed, so RndSeed() reads 4660 before any Rnd or Rand call. That is why an unseeded game produces identical "random" results on every launch; the usual fix is SeedRnd MilliSecs() during start-up.

See also: SeedRnd, Rnd, Rand, MilliSecs.

Example

; RndSeed Example
; ---------------

; RndSeed reads back the random generator's current state.
; A fresh program always starts from the same built-in seed, which
; is why an unseeded game rolls the same "random" numbers every run.

Print "Seed at startup      : "+RndSeed()

; SeedRnd sets the state, RndSeed reads it back
SeedRnd 2024
Print "After SeedRnd 2024   : "+RndSeed()

; Every Rnd or Rand call moves the state on to the next number
roll=Rand(1,100)
Print "Rand(1,100) gave "+roll+", state is now "+RndSeed()

Print ""

; The save-and-restore trick: remember the seed, roll some numbers,
; then put the generator back exactly where it was. Useful when a
; preview or a replay must not disturb the real game's rolls.
saved=RndSeed()
Print "Saved state "+saved
Print "Rolls        : "+Rand(1,6)+" "+Rand(1,6)+" "+Rand(1,6)

SeedRnd saved
Print "Same rolls   : "+Rand(1,6)+" "+Rand(1,6)+" "+Rand(1,6)

Print ""
Print "Press any key to close the example"
WaitKey

End

Index