Sample code for 30+ languages & platforms
Delphi ActiveX

Generating Random ASCII Strings

See more PRNG Examples

Demonstrates how to generate random us-ascii strings.

Chilkat Delphi ActiveX Downloads

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

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
fortuna: TChilkatPrng;
bDigits: Integer;
bLowercase: Integer;
bUppercase: Integer;
i: Integer;

begin
success := 0;

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

success := 0;

fortuna := TChilkatPrng.Create(Self);

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

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

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

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

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