Go to file
R. Eric Wheeler c17e79a982 Update README.md 2020-05-12 16:49:53 +00:00
tests Added LinuxRandom class, seperate URandom class 2016-05-07 10:02:44 -07:00
.gitignore first commit 2016-05-04 10:28:06 -07:00
LICENSE first commit 2016-05-04 10:28:06 -07:00
README.md Update README.md 2020-05-12 16:49:53 +00:00
Renegade.Random.BSDRandom.pas Implement arc4random_buf on bsd 2016-05-05 22:41:42 -07:00
Renegade.Random.Generic.pas Added GetSystemBytes for BSD Unit 2016-05-04 14:34:19 -07:00
Renegade.Random.LinuxRandom.pas Added LinuxRandom class, seperate URandom class 2016-05-07 10:02:44 -07:00
Renegade.Random.RandomInterface.pas Foobar 2016-05-04 11:02:09 -07:00
Renegade.Random.URandom.pas Foobar 2016-05-04 11:02:09 -07:00
Renegade.Random.WinRandom.pas first commit 2016-05-04 10:28:06 -07:00
Renegade.Random.pas Added LinuxRandom class, seperate URandom class 2016-05-07 10:02:44 -07:00

README.md

Free Pascal Random Bytes

This file is a part of Renegade BBS.

This will only work with Free Pascal version 3+. I Think namespacing is a good idea, and FPC 3+ is not hard to get your hands on for any platform. In fact, I believe its the most cross platform program that I have ever found, including ScummVM.

From the Free Pascal website :

Free Pascal is a 32, 64 and 16 bit professional Pascal compiler. It can target many processor architectures: Intel x86 (including 8086), AMD64/x86-64, PowerPC, PowerPC64, SPARC, ARM, AArch64, MIPS and the JVM. Supported operating systems include Linux, FreeBSD, Haiku, Mac OS X/iOS/iPhoneSimulator/Darwin, DOS (16 and 32 bit), Win32, Win64, WinCE, OS/2, MorphOS, Nintendo GBA, Nintendo DS, Nintendo Wii, Android, AIX and AROS. Additionally, support for the Motorola 68k architecture is available in the development versions.

Usage

Make sure to include {$mode objfpc}{$H+} in your header or compile with -S2 -Sh switches.

Program RandomTest;
{$mode objfpc}{$H+}

uses
  Renegade.Random,
  Classes;

var
  R : TRandom;
  S : AnsiString;
  T : TBytes;
begin
  R := TRandom.Init;
  S := R.GetString(22);
  T := R.GetBytes(22);
  R.Free;

You can also use a custom random generator. It needs to implement Renegade.Random.RandomInterface;

Program RandomTest;
{$mode objfpc}{$H+}

uses
  Renegade.Random,
  MyCustomGenerator,
  Classes;

var
  R : TRandom;
  S : AnsiString;
  T : TBytes;
begin
  R := TRandom.Init;
  R.RandomGenerator := MyCustomGenerator.Create;
  S := R.GetString(22);
  T := R.GetBytes(22);
  R.SetDefaultGenerator; // To get back to the default Generator.
  S := R.GetString(22);
  T := R.GetBytes(22);
  R.Free;
  • On Linux systems TRandom uses SYS_getrandom.
  • On Windows systems TRandom uses Windows built in CryptGenRandom function.
  • On BSD systems TRandom uses arc4random_buf this is the same on Mac systems, because Mac = FreeBSD).

TRandom will fall back on reading from /dev/urandom, then /dev/random, and finally Free Pascal's Random function which uses the Mersenne Twister algorithm to get random bytes.