Olive.Random/README.md

60 lines
1.7 KiB
Markdown
Raw Permalink Normal View History

2016-05-04 10:28:06 -07:00
# Free Pascal Random Bytes
2020-05-13 09:28:54 -07:00
This file is a part of Olive BBS.
2016-05-04 10:28:06 -07:00
2020-05-13 09:29:24 -07:00
This will only work with [Free Pascal](http://freepascal.org "Free Pascal") version 3+.
2016-05-04 10:28:06 -07:00
### Usage
Make sure to include ```{$mode objfpc}{$H+}``` in your header or compile with ```-S2 -Sh``` switches.
```pascal
Program RandomTest;
{$mode objfpc}{$H+}
uses
2020-05-13 09:28:54 -07:00
Olive.Random,
2016-05-04 10:28:06 -07:00
Classes;
var
R : TRandom;
S : AnsiString;
T : TBytes;
begin
R := TRandom.Init;
S := R.GetString(22);
T := R.GetBytes(22);
R.Free;
```
2020-05-13 09:28:54 -07:00
You can also use a custom random generator. It needs to implement Olive.Random.RandomInterface;
2016-05-04 10:28:06 -07:00
```pascal
Program RandomTest;
{$mode objfpc}{$H+}
uses
2020-05-13 09:28:54 -07:00
Olive.Random,
2016-05-04 10:28:06 -07:00
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;
```
2020-05-12 09:49:53 -07:00
* On Linux systems TRandom uses SYS_getrandom.
2016-05-04 10:28:06 -07:00
* On Windows systems TRandom uses Windows built in [CryptGenRandom](https://msdn.microsoft.com/en-us/library/windows/desktop/aa379942(v=vs.85).aspx "CryptGenRandom") function.
2020-05-12 09:43:56 -07:00
* On BSD systems TRandom uses [arc4random_buf](https://www.freebsd.org/cgi/man.cgi?query=arc4random_buf&sektion=3 "arc4random_buf") this is the same on Mac systems, because Mac = FreeBSD).
2016-05-04 10:28:06 -07:00
2020-05-12 09:49:53 -07:00
TRandom will fall back on reading from /dev/urandom, then /dev/random, and finally Free Pascal's [Random](http://www.freepascal.org/docs-html/rtl/system/random.html "Random") function which uses the [Mersenne Twister](https://en.wikipedia.org/wiki/Mersenne_Twister "Mersenne Twister") algorithm to get random bytes.