Previous     Next

Using to Generate Truly Random Numbers in .Net

Random numbers can be a pain if you have to generate new sequences each day. The normal ‘randomize()’ statement is seeded from the system timer so if you are running the generator at the same time, then there is a chance you will get the same sequence.

Over at, Mads Haahr has been generating random numbers using atmospheric noise since 1998, and his method has the pseudo-random number generators build into VB.Net beat.

Here is the VB.Net wrapper to call and get your numbers:

Imports System.Net

Public Class clsRandom
        Public Sub RandomizeReseed()
                Dim iSeed(0) As Integer

                iSeed = RandomIntegers(-1000000000, 1000000000, 1)
        End Sub

        Public Function RandomIntegers(ByVal MinValue As Integer, _
                ByVal MaxValue As Integer, _
                ByVal NumberCount As Integer) As Integer()

                Dim iC As Integer
                Dim iRetInts As Integer()
                Dim sRetIntsAsStr As String()
                Dim sURL As String

                If Math.Abs(MinValue) > 1000000000 Then
                        Dim ex As New Exception("MinValue cannot be greater than 1,000,000,000 or less than -1,000,000,000")
                        Throw ex
                End If

                If Math.Abs(MaxValue) > 1000000000 Then
                        Dim ex As New Exception("MaxValue cannot be greater than 1,000,000,000 or less than -1,000,000,000")
                        Throw ex
                End If

                If MinValue > MaxValue Then
                        Dim ex As New Exception("MinValue cannot be greater than MaxValue")
                        Throw ex
                End If

                If NumberCount > 10000 Then
                        Dim ex As New Exception("NumberCount cannot be greater than 10,000")
                        Throw ex
                End If

                sURL = ""
                sURL = sURL & "num=" & NumberCount
                sURL = sURL & "&min=" & MinValue
                sURL = sURL & "&max=" & MaxValue
                sURL = sURL & "&col=1&base=10&format=plain&rnd=new"

                sRetIntsAsStr = CallRandomDotOrg(sURL)
                ReDim iRetInts(sRetIntsAsStr.GetUpperBound(0))
                For iC = 0 To sRetIntsAsStr.GetUpperBound(0)
                        iRetInts(iC) = sRetIntsAsStr(iC)

                RandomIntegers = iRetInts
        End Function

        Private Function CallRandomDotOrg(ByVal sGetURL As String) As String()
                Dim objWebClient As New WebClient()

                Dim sResponse As String
                Dim sReturnInts() As String

                sResponse = objWebClient.DownloadString(sGetURL)
                If Right$(sResponse, 1) = vbLf Then
                        sResponse = Left$(sResponse, Len(sResponse) - 1)
                End If

                sReturnInts = sResponse.Split(vbLf)

                CallRandomDotOrg = sReturnInts
        End Function
End Class

Using the class is pretty simple. You can get an array of integers using the function:
RandomIntegers(MinValue, MaxValue, Count)

or you can seed the built-in random number generator with a truly random number by calling

This post brought to you by - Over 10,000 Aged domains for sale!

Related posts:

  1. SEOMoz Linkscape API wrapper in VB.Net
  2. Getting Flights info from the Kayak API in ASP.Net
  3. Create a Website Comparison Widget using GoogleTrends

If you liked this, then subscribe to my RSS feed