Blitz3D+ Command Reference

Rand ( from[,to] )

Parameters

from - one end of the range

to (optional) - the other end of the range; 1 (default)

Description

Returns a random whole number between the two values, including both ends.

Rand( 1,6 ) rolls a die. Rand( 0,255 ) picks a colour channel. Because the default for the second value is 1, calling Rand with a single number still gives a sensible range: Rand( 6 ) is the same as Rand( 6,1 ) and returns 1 to 6.

The order does not matter - if the second value is smaller than the first they are swapped internally, so Rand( 10,1 ) and Rand( 1,10 ) behave the same.

Use Rand whenever you want a count, an index or a discrete choice: which powerup drops, how many enemies spawn this wave, which of eight taunt lines to play. When you need a fractional value - a position, an angle, a delay - use Rnd instead.

The generator starts from the same fixed seed every run, so an unseeded program produces exactly the same "random" numbers each time you launch it. That is handy while debugging, and a nasty surprise when you ship, so call SeedRnd with something that changes - MilliSecs() is the usual choice - once at startup.

See also: Rnd, SeedRnd, RndSeed, MilliSecs.

Example

; Rand Example
; ------------

; Rand returns a random INTEGER between two values, both inclusive.
; With one argument the low value defaults to 1.

; Seed with the clock so each run rolls differently
SeedRnd MilliSecs()

Print "Rolling two six-sided dice five times:"
For roll=1 To 5
    die1=Rand(1,6)
    die2=Rand(1,6)
    Print "Roll "+roll+": "+die1+" + "+die2+" = "+(die1+die2)
Next

Print ""
Print "Rand(100) uses the default low value of 1: "+Rand(100)
Print ""
Print "(Need random fractions instead? See Rnd.)"

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

End

Index