Tuesday, August 31, 2010

Visual Basic Math Function : Rnd

Purpose : The Rnd function returns a single-precision random number between 0 and 1 .

Syntax :
Rnd [ ( lngExpression ) ]

lngExpression : A value that determines which number in the pseudo-random sequence to return ( optional )

Function RndInt ( intLoNum as Integer , intHiNum as Integer ) as Integer
Static blnSeeded as Boolean
Dim intRange as Integer
if blnSeeded = False Then
Randomize
blnSeeded = True
Enf if
intRange = intHiNum - intLoNum + 1
RndInt = Int ( intRange * Rnd + intLoNum )
End Function

This example is a function that uses the Randomize statement and Rnd function to return an integer in the range specified by the integer parameters intLoNum and intHiNum.

No comments:

Post a Comment