Sample code for 30+ languages & platforms
Delphi DLL

Fortuna PRNG Generate Random Encoded

See more PRNG Examples

Demonstrates how to generate random bytes using the Fortuna PRNG. The random bytes are returned in an encoded string (using an encoding such as hex, base64, base58, etc.)

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Prng;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
fortuna: HCkPrng;
strEntropy: PWideChar;
strRandHex: PWideChar;
strRandBase64: PWideChar;
strRandBase58: PWideChar;

begin
success := False;

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

success := False;

fortuna := CkPrng_Create();

// Before beginning to generate random data, 
// the PRNG (Pseudo Random Number Generator) should
// be seeded with real random data (also known as "entropy").

// Note: Accumulating real random data can be difficult
// and time-consuming to collect.  It is for this reason
// that pseudorandom data (i.e. a PRNG) is used.  The pseudorandom data generator
// is seeded with entropy.  In addition, new entropy can (and should)
// be periodically added as more pseudorandom data is generated.

// Get 32 bytes of system entropy.  On Linux/Unix systems, this reads
// from /dev/random.  On MS Windows systems, it uses the Crypto API's 
// CryptGenRandom function.
strEntropy := CkPrng__getEntropy(fortuna,32,'hex');
if (CkPrng_getLastMethodSuccess(fortuna) <> True) then
  begin
    Memo1.Lines.Add(CkPrng__lastErrorText(fortuna));
    Exit;
  end;

// Seed the PRNG with this entropy:
success := CkPrng_AddEntropy(fortuna,strEntropy,'hex');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkPrng__lastErrorText(fortuna));
    Exit;
  end;

// Generate some random data:
strRandHex := CkPrng__genRandom(fortuna,16,'hex');
strRandBase64 := CkPrng__genRandom(fortuna,22,'base64');
strRandBase58 := CkPrng__genRandom(fortuna,32,'base58');

Memo1.Lines.Add('hex random bytes: ' + strRandHex);
Memo1.Lines.Add('base64 random bytes: ' + strRandBase64);
Memo1.Lines.Add('base58 random bytes: ' + strRandBase58);

CkPrng_Dispose(fortuna);

end;