Rnd Function


Rnd Function

Class

Microsoft.VisualBasic.VBMath

Syntax

     Dim result As Single = Rnd[(number)] 


number (optional; Single)

The pseudorandom-number generator seed value. The following list describes how the Rnd function interprets number.

Number

Rnd generates

Negative

The same number each time, using number as the seed number

Positive

The next random number in the current sequence

Zero

The most recently generated number

Not Supplied

The next random number in the current sequence


Description

The Rnd function returns a random number between 0 and 1, inclusive.

Usage at a Glance

  • Before calling the Rnd function, you should use the Randomize procedure to initialize the random-number generator.

  • The standard formula for producing random numbers in a given range is:

         result = Int((highest - lowest + 1) * Rnd(  ) + lowest) 

    where lowest is the lowest required number in the range and highest is the highest.

Example

The following example returns an array of 100 random numbers.

     Public Function GenerateRandomNumbers(  ) As Single(  )        Dim results As Single(0 To 99)        Dim counter As Integer        Randomize(  )        For counter = 0 To 99           results(counter) = Rnd(  )        Next counter        Return results     End Function 

See Also

Randomize Procedure




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net