How to Do Random Numbers in Visual Basic

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Do Random Numbers in Visual Basic
Generate random binary numbers in VB. ( Images)

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.

Skill level:
Moderately Easy

Other People Are Reading


  1. 1

    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.

  2. 2

    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

  3. 3

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

        Dim randomNums As New Random()
  4. 4

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

  5. 5

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

        Console.WriteLine("Random Numbers:")
        For i = 0 To 149
            Console.WriteLine(my Bytes(i))
        Next i
  6. 6

    Press "F5" to run the program.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.