Blitz3D+ Command Reference

SeedRnd seed

Parameters

seed - the integer value to restart the random number generator from

Description

Sets the starting point of the random number generator.

Blitz3D+ random numbers are not really random - they are a fixed sequence worked out from a seed. Give it the same seed and you get the same numbers in the same order, every time. A fresh program always begins from the same built-in seed, so without SeedRnd your "random" level layout is identical on every launch.

The standard fix is one line near the top of your game: SeedRnd MilliSecs(). The clock has moved on since last time, so each run gets a different sequence.

Repeatability is a feature too, though. Seed from a fixed number and a procedurally generated dungeon, planet or race track comes out the same every time - so you can save a level as nothing but its seed, share it as a short code, or reproduce a bug report exactly. Reseeding partway through a game restarts the sequence from that point.

Only the low 31 bits of the seed are used, and a seed of 0 is treated as 1, so negative values and zero are all perfectly safe to pass. Use RndSeed to read the generator's current state back.

See also: Rnd, Rand, RndSeed, MilliSecs.

Example

; SeedRnd Example
; ---------------

; SeedRnd sets the starting point of the random number generator.
; The same seed always produces the SAME sequence - useful for
; repeatable tests, or procedural levels you can revisit by number.

SeedRnd 1234
Print "Seed 1234, first try : "+Rand(1,100)+" "+Rand(1,100)+" "+Rand(1,100)

SeedRnd 1234
Print "Seed 1234, second try: "+Rand(1,100)+" "+Rand(1,100)+" "+Rand(1,100)

Print "Same seed, same numbers - every run of this example too."

Print ""

; Seeding with the clock gives a fresh sequence every run - do this
; once at the start of a game for unpredictable results
SeedRnd MilliSecs()
Print "Seed MilliSecs()     : "+Rand(1,100)+" "+Rand(1,100)+" "+Rand(1,100)
Print "(different every time you run the example)"

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

End

Index