RANDOM.ORG is a true random number generating web service. It uses atmospheric noise to generate randomness. Here we will consume parts of their API using VBA.
Although there are four parts to the RANDOM.ORG API, we will only be looking at two:
Integer Generator
String Generator
Integer Generator
The Integer Generator will generate truly random integers in configurable intervals. It is pretty easy to write a client to access the integer generator. The integer generator accepts only HTTP GET requests, so parameters are passed via encoding in the URL.
We start out by declaring the base URL for all integer API requests:
Public Const NUMBER_BASE_URL As String = "http://www.random.org/integers/?"
In this example I use the smallest and largest available decimal values, to make things easy. You may want to allow for the calling procedure to specify its own range of values, or choose a different base. In that case, you'll need to alter this function.
Function GetRandomNumbers(howMany As Integer) As Long()
' http://www.random.org/clients/http/
Dim xml As Object ' MSXML2.XMLHTTP60
Dim tempLng() As Long
Dim tempString() As String
Dim result As String
Dim i As Long, j As Long
Set xml = GetMSXML
With xml
.Open "GET", NUMBER_BASE_URL & "num=" & howMany & _
"&min=-1000000000&max=1000000000&col=1&base=10&format=plain&rnd=new", False
.Send
End With
result = xml.responseText
' split query results into string array
tempString = Split(result, Chr(10))
' resize tempLng to same size as tempString
ReDim tempLng(LBound(tempString) To (UBound(tempString) - 1))
' convert string array into long array
For i = LBound(tempString) To (UBound(tempString) - 1)
tempLng(i) = CLng(tempString(i))
Next i
GetRandomNumbers = tempLng
End Function
What we do is take the query result (which is a series of random numbers separated by carriage returns) and use the Split function to put each number into an array. Then we convert the number from a String type to Long type.
The GetMSXML function may be found on the MSXML Object Library page.
Sample Usage
Sub GetNums() Dim ints() As Long ints = GetRandomNumbers(10) Debug.Print ints(0) End Sub
String Generator
The String Generator will generate truly random strings of various length and character compositions. It is pretty easy to write a client to access the string generator. The string generator accepts only HTTP GET requests, so parameters are passed via encoding in the URL.
First we declare the base URL for all string API requests:
Public Const STRING_BASE_URL As String = "http://www.random.org/strings/?"
The function to generate strings from the API is essentially the same as the function above. In my example I use the maximum allowable string length, and I request upper/lower case and numbers as well. You may want to parameterize these options instead.
Function GetRandomStrings(howMany As Integer) As String()
' http://www.random.org/clients/http/
Dim xml As Object ' MSXML2.XMLHTTP60
Dim tempString() As String
Dim result As String
Dim i As Long, j As Long
Set xml = GetMSXML
With xml
.Open "GET", STRING_BASE_URL & "num=" & howMany & _
"&len=20&digits=on&upperalpha=on&loweralpha=on&unique=off&format=plain&rnd=new", _
False
.Send
End With
result = xml.responseText
' split query results into string array
tempString = Split(result, Chr(10))
GetRandomStrings = tempString
End Function
Because we are dealing with strings, all we need to do is Split the output into an array.
Sample Usage
Sub GetNums() Dim strs() As String strs = GetRandomStrings(10) Debug.Print strs(0) End Sub
I wrote an Excel add-in that generates random numbers as well as other demographic points. Visit Random Data Generator for more information.
