How to Do Random Numbers in Visual Basic

Updated July 20, 2017

At times you may need to generate random numbers to fill an array in your Visual Basic applications for testing purposes. Use the "Random" class to generate numbers that meet certain statistical requirements for randomness. The "Random.NextBytes" method fills the elements of a specified array of bytes with random numbers. You can use random numbers to test saving binary data to a file or to a database table without having to read a real file.

Launch Microsoft Visual Basic Express, click "New Project..." on the left pane of your screen then select "Console Application." Click "OK" to create a new console project.

Create the "Byte" data type array to fill with random numbers by typing the following line of code (capitalisation matters):

Dim my Bytes(150) As Byte

Create the "Random" data type variable to fill the array with random byte values by adding the following line of code:

Fill the byte array with random numbers by adding the following line:

Create a loop and display the random byte values in the array by typing the following code:

Press "F5" to run the program.

Cite this Article A tool to create a citation to reference this article Cite this Article

About the Author

Jaime Avelar is a professional writer whose programming articles appear on various websites. He has been a software programmer since 2000. Avelar holds a Master of Science in information systems from the University of Texas at Arlington.