Deprecated: Assigning the return value of new by reference is deprecated in /home/gathadam/public_html/wp-settings.php on line 520

Deprecated: Assigning the return value of new by reference is deprecated in /home/gathadam/public_html/wp-settings.php on line 535

Deprecated: Assigning the return value of new by reference is deprecated in /home/gathadam/public_html/wp-settings.php on line 542

Deprecated: Assigning the return value of new by reference is deprecated in /home/gathadam/public_html/wp-settings.php on line 578

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/gathadam/public_html/wp-settings.php on line 18
2008 June

Using Random.org 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   Random.org, 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 random.org and get your numbers:


Imports System.Net

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

                iSeed = RandomIntegers(-1000000000, 1000000000, 1)
                Randomize(iSeed(0))
        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 = "http://www.random.org/integers/?"
                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)
                Next

                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
RandomizeReseed()

This post brought to you by WeGotDomain.com - 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