The following examples show how to use Delphi to download random positive integers within a range from an AlphaRNG device through the Entropy Server for Windows. The code only works while the AlphaRNG Entropy Server is running. Before using the examples, make sure the AlphaRNG device is plugged into an available USB port and entropy-server.exe is running on the same system.
Retrieving a random positive integer within a range through a named pipe
//
//
// 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.
Retrieving a random positive integer within a range through 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.