The following VBA Excel macros download random data from the SwiftRNG entropy server when used with 32-bit Microsoft Excel. Before using the examples, make sure a SwiftRNG device is plugged in and the SwiftRNG entropy server is running.
Retrieving a random VBA Integer into cell A1
' Include the exact path to SwiftRNG-32.dll which is available with the software kit.
' Make sure that the SwiftRNG Entropy Server is running
Declare Function getRandomByte Lib "C:\tools\windows-binaries\windows-x86\SwiftRNG-32.dll" _
Alias "getEntropyAsByte" () As Integer
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
ByRef Destination As Any, _
ByRef Source As Any, _
ByVal Length As Long)
Private Function getRandomInteger() As Integer
Dim RandomBytes(1 To 2) As Byte
Dim i As Integer
Dim rndInteger As Integer
Dim rndByte As Integer
For i = 1 To LenB(rndInteger)
rndByte = getRandomByte()
If rndByte > 255 Then
Err.Raise Number:=1, Description:="Could not retrieve random Integer from SwiftRNG Entropy Server"
Else
RandomBytes(i) = rndByte
End If
Next
CopyMemory rndInteger, RandomBytes(1), LenB(rndInteger)
getRandomInteger = rndInteger
End Function
Sub Macro1()
Dim rndInteger As Integer
rndInteger = getRandomInteger()
[A1].Value = rndInteger
End Sub
Retrieving a random VBA Double into cell A1
' Include the exact path to SwiftRNG-32.dll which is available with the software kit.
' Make sure that the SwiftRNG Entropy Server is running
Declare Function getRandomByte Lib "C:\tools\windows-binaries\windows-x86\SwiftRNG-32.dll" _
Alias "getEntropyAsByte" () As Integer
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
ByRef Destination As Any, _
ByRef Source As Any, _
ByVal Length As Long)
Private Function getRandomDouble() As Double
Dim RandomBytes(1 To 8) As Byte
Dim i As Integer
Dim d As Double
Dim rndByte As Integer
For i = 1 To 8
rndByte = getRandomByte()
If rndByte > 255 Then
Err.Raise Number:=1, Description:="Could not retrieve random double from SwiftRNG entropy server"
Else
RandomBytes(i) = rndByte
End If
Next
CopyMemory d, RandomBytes(1), LenB(d)
getRandomDouble = d
End Function
Sub Macro1()
Dim randomDouble As Double
randomDouble = getRandomDouble()
[A1].Value = randomDouble
End Sub