Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Generating Random ASCII Strings

See more PRNG Examples

Demonstrates how to generate random us-ascii strings.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Prng;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  fortuna: TPrng;
  bDigits: Boolean;
  bLowercase: Boolean;
  bUppercase: Boolean;
  i: Integer;

begin
  success := False;

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

  success := False;

  fortuna := TPrng.Create;

  //  Generate random strings having only lowercase chars (a-z)
  //  Disallow digits and uppercase and only allow lowercase
  bDigits := False;
  bUppercase := False;
  bLowercase := True;

  WriteLn('-- only lowercase alpha (a-z)');
  for i := 1 to 10 do
    begin
      //  Generate 20-character strings.
      WriteLn(fortuna.RandomString(20,bDigits,bLowercase,bUppercase));
    end;

  //  Allow both lowercase and uppercase alpha chars
  bUppercase := True;
  WriteLn('-- lower and uppercase alpha (a-zA-Z)');
  for i := 1 to 10 do
    begin
      //  Generate 20-character strings.
      WriteLn(fortuna.RandomString(20,bDigits,bLowercase,bUppercase));
    end;

  //  Allow digits (0-9)
  bDigits := True;
  WriteLn('-- digits and lower/uppercase alpha (0-9a-zA-Z)');
  for i := 1 to 10 do
    begin
      //  Generate 20-character strings.
      WriteLn(fortuna.RandomString(20,bDigits,bLowercase,bUppercase));
    end;

  //  Allow only digits (0-9)
  bUppercase := False;
  bLowercase := False;
  WriteLn('-- only digits (0-9)');
  for i := 1 to 10 do
    begin
      //  Generate 20-character strings.
      WriteLn(fortuna.RandomString(20,bDigits,bLowercase,bUppercase));
    end;



  fortuna.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.