SiteMap  | About    

Random Number Generator in VB.NET

In some situations we have to generate Random numbers in our VB.NET applications for different purposes. In these situations we have to use Random Class for generating the Random numbers. The Random Class in VB.NET represents a pseudo-random number generator, a device that produces a sequence of numbers that meet certain statistical requirements for randomness.

  Shared random As New Random()

The Next() method in Random class returns a nonnegative random number.


We can limit the generation of Random number by giving a specified range to Next() method.


The above code limit to generate the Random number within the range from 10 to 20.

To improve performance, create one Random object to generate many random numbers over time, instead of repeatedly creating a new Random objects to generate one random number. The following VB.NET program generate Random numbers within the specified rand from 10 to 20.

Public Class Form1
    Shared random As New Random()
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        For i = 0 To 5
            MsgBox(Convert.ToString(random.Next(10, 20)))
    End Sub
End Class (C) 2015    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.