Home

Visual Basic Built-In Functions: Random Functions

  

Description

A random number is a value that is not known in advanced until it is generated by the compiler. To assist you with getting a random number, the Visual Basic language provides a function named Rnd. Its syntax is:

Public Shared Function Rnd[(Number)] As Single

This function takes an optional argument. If the argument is not passed, the compiler would simply generate a positive decimal number between 0 and 1. Here is an example:

Public Module Exercise

    Public Function Main() As Integer
        
        MsgBox("Random Number: " & Rnd())

        Return 0
    End Function

End Module

This would produce:

Random Number

You may wonder how the compiler generates a random number. Without going into all the details, in most cases, a compiler refers to the system clock (the clock of the computer on which the application is). It uses a certain algorithm to get that number.

If you call the function like we did above, every time you execute the application, you are likely to get the same result. Depending on how you want to use the number, in some cases, you may want to get a different number every time. To support this, random arithmetic supports what is referred to as a seed. If you do not use a seed, the compiler would keep the same number it generated from the system clock the first time it was asked to produce a random number. Seeding allows the compiler to reset this mechanism, which would result in a new random number.

To assist you with seeding, the Visual Basic language provides a function named Randomize. Its syntax is:

Public Shared Sub Randomize ([ Number ])

This function takes one optional argument. If you can this function without the argument, the compiler would refer to the system clock to generate the new random number. Of course, to get the number, you must call this function before calling Rnd(). Here is an example:

Public Module Exercise

    Public Function Main() As Integer
        Randomize()

        MsgBox("Random Number: " & Rnd())

        Return 0
    End Function

End Module

This time, every time the Rnd() function is called, the compiler generates a new number. Instead of letting the compiler refer to the system clock, you can provide your own seed value. To do this, pass a number to the Randomize() function.

We mentioned that the Rnd() function generates a number between 0 and 1. Of course, in some cases you will want the number to be in a higher range, such as between 0 and 100 or between 0 and 100000. All you have to do is to multiply the result to a number of your choice. Here is an example:

Public Module Exercise

    Public Function Main() As Integer
        Randomize()

        MsgBox("Random Number: " & CStr(100 * Rnd()))

        Return 0
    End Function

End Module

This would produce:

Random Number

Also notice that the result is a decimal number. If you interested in only the integral part of the number, you can call the Int() function.

Besides Visual Basic's own combination of the Rnd() and the Randomize() functions, the .NET Framework supports random numbers in another way (using Random).
 
 
     
 

Home Copyright © 2008-2016, FunctionX, Inc.