The following demonstrates how to use Delphi language to download random positive integer numbers within a range from an AlphaRNG device when used with Entropy Server for Windows.
The following code will only work when running AlphaRNG Entropy Server.
Before using the following example, make sure the AlphaRNG device is plugged into one of USB ports available and
entropy-server.exe
is running on same system.
A Delphi sample source code for retrieving random positive integer values within a range from an AlphaRNG device when using Entropy Server for Windows.
//
//
// A sample Delphi program that demonstrates how to retrieve positive random numbers
// within a [10...20] range from AlphaRNG device using Entropy Server for Windows.
//
//
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
SysUtils,
Classes;
var
EntropyServerStream: TFileStream;
ByteCount: LongWord;
DwnldRndNumber: LongWord;
FinalRandNumber: LongWord;
MinRandomValue: LongWord;
MaxRandomValue: LongWord;
RequestByteArray: array[0..7] of Byte;
begin
EntropyServerStream := TFileStream.Create('\\.\pipe\AlphaRNG', fmOpenReadWrite);
try
// Set the random expected range
MinRandomValue := 10;
MaxRandomValue := 20;
// How many random bytes to retrieve
ByteCount := SizeOf(DwnldRndNumber);
// Prepare the entropy server request
// For more information visit https://tectrolabs.com/docs/alpharng/entropy-server-api/
Move(ByteCount, RequestByteArray[4], SizeOf(ByteCount));
// Send request to the entropy server.
// Request bytes should be sent at once with no delay in between.
EntropyServerStream.WriteBuffer(RequestByteArray, SizeOf(RequestByteArray));
// Retrieve random bytes from the entropy server.
// Random bytes should be received at once with no delay in between.
EntropyServerStream.ReadBuffer(DwnldRndNumber, SizeOf(DwnldRndNumber));
// Adjust to expected range
FinalRandNumber := MinRandomValue + (DwnldRndNumber mod (MaxRandomValue - MinRandomValue + 1));
finally
EntropyServerStream.Free;
end;
// Print the final random number
Writeln('random integer: ', FinalRandNumber);
end.
A Delphi sample source code for retrieving random positive integer values within a range from an AlphaRNG device using Entropy Server and AlphaRNG-32.dll
//
//
// A sample Delphi program that demonstrates how to retrieve positive random numbers
// within a [10...20] range from AlphaRNG device using AlphaRNG-32.dll
//
//
//
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
type
DwnldRndNumberType = LongWord;
xDwnldRndNumberType = ^DwnldRndNumberType;
var
DwnldRndNumber: DwnldRndNumberType;
PDwnldRndNumber: ^LongWord;
FinalRandNumber: LongWord;
MinRandomValue: LongWord;
MaxRandomValue: LongWord;
Status: Integer;
Function getEntropy(p: xDwnldRndNumberType; y: longword): integer; stdcall; external 'C:\tools\alrng-windows-binaries\windows-x86-vs-2019\AlphaRNG-32.dll';
begin
try
// Set the random expected range
MinRandomValue := 10;
MaxRandomValue := 20;
Status := getEntropy(@DwnldRndNumber, SizeOf(DwnldRndNumber));
if Status = 0 then
begin
// Adjust to expected range
FinalRandNumber := MinRandomValue + (DwnldRndNumber mod (MaxRandomValue - MinRandomValue + 1));
// Print the final random number
Writeln('random integer: ', FinalRandNumber);
end
else
Writeln('status in error: ', Status);
except
on E: Exception do
writeln(E.ClassName, ': ', E.Message);
end;
end.