Rnd# ( from#[,to#] )
Parameters
|
from - one end of the range to (optional) - the other end of the range; 0 (default) |
Description
|
Returns a random float between the two values. Rnd is the fractional partner to Rand. Rnd( 0,1 ) gives you the classic 0-to-1 roll to compare against a probability; Rnd( -180,180 ) picks a heading; Rnd( 0.5,2.0 ) picks a pitch multiplier for a sound so repeated hits do not sound identical. The second value defaults to 0, so Rnd( 10 ) means "between 10 and 0" and returns a float somewhere in that span. Written with one argument it reads a little oddly, but it is the common shorthand for "a random amount up to this much". Assign the result to a # variable. Storing it in an integer converts it with Int rounding and throws away exactly the precision you asked for. As with Rand, the sequence is identical on every run until you call SeedRnd, so seed once at startup from something that varies. See also: Rand, SeedRnd, RndSeed, Float. |
Example
; Rnd Example ; ----------- ; Rnd returns a random FLOAT between two values - use it for amounts ; that need fractions, like speeds and angles. (Compare Rand, which ; deals in whole integers.) ; Seed with the clock so each run differs SeedRnd MilliSecs() Print "Five random floats from Rnd(0,10):" For i=1 To 5 Print " "+Rnd(0,10) Next Print "" ; Typical game use: launch a spark in a random direction at a ; slightly varied speed angle#=Rnd(0,360) speed#=Rnd(2.5,4.0) Print "A spark flies off at "+angle+" degrees, speed "+speed Print "" Print "Press any key to close the example" WaitKey End
Index