The following VBA Excel macros download random data from a SwiftRNG device through a named pipe connection to the entropy server for Windows. The code only works while the SwiftRNG entropy server is running.
This is the recommended way to achieve maximum concurrent speed and performance.
Before using the examples, make sure the SwiftRNG device is plugged into an available USB port and entropy-server.exe is running on the same system.
Retrieving a random VBA Integer into cell A1
Private Function getRandomInteger() As Integer
Dim intFileNum%
intFileNum = FreeFile
Dim RequestHeaderBytes(1 To 4) As Integer
Dim randomInteger As Integer
' Open the entropy server named pipe
Open "\\.\pipe\SwiftRNG" For Binary Access Read Write As intFileNum
' Prepare the Entropy Server API request structure - requesting 2 bytes for the VBA Integer.
' For more information visit https://tectrolabs.com/docs/swiftrng/entropy-server-api/
RequestHeaderBytes(3) = LenB(randomInteger)
' Send the request header to the entropy server in one shot
Put intFileNum, , RequestHeaderBytes
' With no delay, receive the random Integer
Get intFileNum, , randomInteger
Close intFileNum
getRandomInteger = randomInteger
End Function
Sub Macro1()
Dim rndInteger As Integer
rndInteger = getRandomInteger()
[A1].Value = rndInteger
End Sub
Retrieving a random VBA Double into cell A1
Private Function getRandomDouble() As Double
Dim intFileNum%
intFileNum = FreeFile
Dim RequestHeaderBytes(1 To 4) As Integer
Dim randomDouble As Double
' Open the entropy server named pipe
Open "\\.\pipe\SwiftRNG" For Binary Access Read Write As intFileNum
' Prepare the Entropy Server API request structure - requesting 8 bytes for the VBA Double.
' For more information visit https://tectrolabs.com/docs/swiftrng/entropy-server-api/
RequestHeaderBytes(3) = LenB(randomDouble)
' Send the request header to the entropy server in one shot
Put intFileNum, , RequestHeaderBytes
' With no delay, receive the random Double
Get intFileNum, , randomDouble
Close intFileNum
getRandomDouble = randomDouble
End Function
Sub Macro1()
Dim rndDouble As Double
rndDouble = getRandomDouble()
[A1].Value = rndDouble
End Sub
Retrieving a random VBA Long into cell A1
Private Function getRandomLong() As Long
Dim intFileNum%
intFileNum = FreeFile
Dim RequestHeaderBytes(1 To 4) As Integer
Dim randomLong As Long
' Open the entropy server named pipe
Open "\\.\pipe\SwiftRNG" For Binary Access Read Write As intFileNum
' Prepare the Entropy Server API request structure - requesting 4 bytes for a VBA Long.
' For more information visit https://tectrolabs.com/docs/swiftrng/entropy-server-api/
RequestHeaderBytes(3) = LenB(randomLong)
' Send the request header to the entropy server in one shot
Put intFileNum, , RequestHeaderBytes
' With no delay, receive the random Long
Get intFileNum, , randomLong
Close intFileNum
getRandomLong = randomLong
End Function
Sub Macro1()
Dim rndLong As Long
rndLong = getRandomLong()
[A1].Value = rndLong
End Sub