The following demonstrates how to use VBA Excel to download random data from a SwiftRNG device when used with Entropy Server
for Windows and utilizing a named pipe. The following code will only work when running SwiftRNG Entropy Server.
This is the recommended way for achieving maximum concurrent speed and performance.
Before using the following example, make sure the SwiftRNG device is plugged into one of USB ports available and
entropy-server.exe
is running on same system.
A sample source code that retrieves a random VBA Integer using entropy-server through a named pipe connection and inserts that value into A1 cell of the Excel Sheet
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
A sample source code that retrieves a random VBA Double using entropy-server through a named pipe connection and inserts that value into A1 cell of the Excel Sheet
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
A sample source code that retrieves a random VBA Long using entropy-server through a named pipe connection and inserts that value into A1 cell of the Excel Sheet
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