True or Real Random Generator Class in VB.Net


Create a Class file as shown

Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks

Namespace vbRand
    Public Interface IRandomizer
        Function Generate(max As Integer) As Integer
    End Interface

    Public Class RealRandom
        Implements IRandomizer
        Public Function Generate(max As Integer) As Integer Implements IRandomizer.Generate
            Return (New Random()).[Next](0, max)
        End Function

        'Private Function IRandomizer_Generate(max As Integer) As Integer Implements IRandomizer.Generate
        '    Return 0
        'End Function

    End Class
End Namespace


Import this class to your code where you wanna generate unique random number.

Imports vbRand


Private Sub btnPost_Click(sender As Object, e As EventArgs) Handles btnPost.Click
        Dim Randomizer As IRandomizer = New RealRandom()
        divMsg.InnerHtml = Randomizer.Generate(100).ToString
    End Sub


Now you will get random number from 0 to 99

- Written By
Vinod Kotiya

Comments