Delphi compatible LCG Random: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | ### Delphi compatible random numbers | |
| 2 | 2 | ||
| 3 | 3 | Many Free Pascal programmers also maintain sourcecode in Delphi.\ | |
| 4 | 4 | Even if you have moved to Free Pascal from Delphi you may have data that relies on Delphi's Random.\ | |
| 5 | 5 | Here are cross-platform functions that generate Delphi-identical pseudo-random numbers given the same RandSeed: | |
| 6 | 6 | ||
| 7 | 7 | ``` pascal | |
| 8 | 8 | unit lcg_random; | |
| 9 | 9 | // Delphi compatible LCG random number generator routines for Free Pascal. | |
| 10 | 10 | // (c)2017, Thaddy de Koning. Use as you like | |
| 11 | 11 | // Algorithm, Delphi multiplier and increment taken from: | |
| 12 | 12 | // https://en.wikipedia.org/wiki/Linear_congruential_generator | |
| 13 | 13 | // The default Delphi RandomSeed is determined as zero. | |
| 14 | 14 | {$ifdef fpc}{$mode objfpc}{$endif} | |
| 15 | 15 | ||
| 16 | 16 | interface | |
| 17 | 17 | ||
| 18 | 18 | function LCGRandom: extended; overload;inline; | |
| 19 | 19 | function LCGRandom(const range:longint):longint;overload;inline; | |
| 20 | 20 | ||
| 21 | 21 | implementation | |
| 22 | 22 | ||
| 23 | 23 | function IM:cardinal;inline; | |
| 24 | 24 | begin | |
| 25 | 25 | RandSeed := RandSeed * 134775813 + 1; | |
| 26 | 26 | Result := RandSeed; | |
| 27 | 27 | end; | |
| 28 | 28 | ||
| 29 | 29 | function LCGRandom: extended; overload;inline; | |
| 30 | 30 | begin | |
| 31 | 31 | Result := IM * 2.32830643653870e-10; | |
| 32 | 32 | end; | |
| 33 | 33 | ||
| 34 | 34 | function LCGRandom(const range:longint):longint;overload;inline; | |
| 35 | 35 | begin | |
| 36 | 36 | Result := IM * range shr 32; | |
| 37 | 37 | end; | |
| 38 | 38 | ||
| 39 | 39 | end. | |
| 40 | 40 | ``` | |
| 41 | 41 | ||
| 42 | - | ||
| 43 | 42 | A small demo | |
| 44 | 43 | ||
| 45 | 44 | ``` pascal | |
| 46 | 45 | program lcgdemo; | |
| 47 | 46 | // Compile and run in both Delphi and FPC and compare the output. | |
| 48 | 47 | // It is exactly the same | |
| 49 | 48 | {$ifdef fpc}{$mode objfpc} | |
| 50 | 49 | {$Macro on}{$define random := LCGRandom} | |
| 51 | 50 | {$endif} | |
| 52 | 51 | {$ifdef mswindows}{$apptype console}{$endif} | |
| 53 | 52 | uses lcg_random; | |
| 54 | 53 | ||
| 55 | 54 | var i:integer; | |
| 56 | 55 | begin | |
| 57 | 56 | RandSeed := 999; // default delphi randseed is zero | |
| 58 | 57 | for i := 1 to 20 do | |
| 59 | 58 | begin | |
| 60 | 59 | write(Random(100):4); // Delphi: Random FPC:LCGRandom. See macro | |
| 61 | 60 | if i mod 5 = 0 then writeln; | |
| 62 | 61 | end; | |
| 63 | 62 | ||
| 64 | 63 | RandSeed := 999; // default delphi randseed is zero | |
| 65 | 64 | for i := 1 to 20 do | |
| 66 | 65 | begin | |
| 67 | 66 | write(Random:4:8,' ');// Delphi: Random FPC:LCGRandom | |
| 68 | 67 | if i mod 5 = 0 then writeln; | |
| 69 | 68 | end; | |
| 70 | 69 | Readln; | |
| 71 | 70 | end. | |
| 72 | 71 | ``` | |
| 73 | 72 | ||
| 74 | 73 | Here is another one with the randseed factored out. You can use that for non-delphi compatible purposes: | |
| 75 | 74 | ||
| 76 | 75 | ``` pascal | |
| 77 | 76 | unit lcg_random2; | |
| 78 | 77 | // Delphi compatible LCG random number generator routines for Freepascal. | |
| 79 | 78 | // (c)2017, Thaddy de Koning. Use as you like | |
| 80 | 79 | // Algorithm, Delphi multiplier and increment taken from: | |
| 81 | 80 | // https://en.wikipedia.org/wiki/Linear_congruential_generator | |
| 82 | 81 | // The default Delphi RandomSeed is determined as zero. | |
| 83 | 82 | // | |
| 84 | 83 | // In this version the system randseed is factored out. | |
| 85 | 84 | {$ifdef fpc}{$mode objfpc}{$endif}{$J+} | |
| 86 | 85 | ||
| 87 | 86 | interface | |
| 88 | 87 | const | |
| 89 | 88 | RandSd:cardinal = 0; | |
| 90 | 89 | ||
| 91 | 90 | function LCGRandom: extended; overload;inline; | |
| 92 | 91 | function LCGRandom(const range:longint):longint;overload;inline; | |
| 93 | 92 | ||
| 94 | 93 | implementation | |
| 95 | 94 | ||
| 96 | 95 | function IM:cardinal;inline; | |
| 97 | 96 | begin | |
| 98 | 97 | RandSd := RandSd * 134775813 + 1; | |
| 99 | 98 | Result := RandSd; | |
| 100 | 99 | end; | |
| 101 | 100 | ||
| 102 | 101 | function LCGRandom: extended; overload;inline; | |
| 103 | 102 | begin | |
| 104 | 103 | Result := IM * 2.32830643653870e-10; | |
| 105 | 104 | end; | |
| 106 | 105 | ||
| 107 | 106 | function LCGRandom(const range:longint):longint;overload;inline; | |
| 108 | 107 | begin | |
| 109 | 108 | Result := IM * range shr 32; | |
| 110 | 109 | end; | |
| 111 | 110 | ||
| 112 | 111 | end. | |
| 113 | 112 | ``` | |
| 114 | 113 | ||
| 115 | 114 | You can use this for example for simple encryption and decryption based on the predictability and repeatability of a any Pseudo Random Number Generator: | |
| 116 | 115 | ||
| 117 | 116 | ``` pascal | |
| 118 | 117 | program cryptrandom; | |
| 119 | 118 | {$mode objfpc}{$modeswitch typehelpers}{$H+} | |
| 120 | 119 | { This is demo that you can actually use random to encrypt/decrypt. | |
| 121 | 120 | This is due to the nature of a Pseudo Random Number Generator, | |
| 122 | 121 | where given a certain random seed, the sequence is predictable. | |
| 123 | 122 | Here I use the Random Seed as a key to encrypt/decrypt a string. | |
| 124 | 123 | The same code would also work with the Default random from FreePascal, | |
| 125 | 124 | but it is better not to touch system.randseed, So I used a different | |
| 126 | 125 | Random. | |
| 127 | 126 | ||
| 128 | 127 | Have fun, Thaddy} | |
| 129 | 128 | uses | |
| 130 | 129 | lcg_random2; | |
| 131 | 130 | ||
| 132 | 131 | ||
| 133 | 132 | function Crypt(const value:AnsiString;const Key:Cardinal):AnsiString; | |
| 134 | 133 | var | |
| 135 | 134 | i:integer; | |
| 136 | 135 | begin | |
| 137 | 136 | RandSd := Key; | |
| 138 | 137 | SetLength(Result,Length(Value)); | |
| 139 | 138 | for i := 1 to length(Value) do | |
| 140 | 139 | Result[i] := Chr(Ord(Value[i]) xor lcgrandom(255)); | |
| 141 | 140 | end; | |
| 142 | 141 | ||
| 143 | 142 | var | |
| 144 | 143 | a:AnsiString; | |
| 145 | 144 | begin | |
| 146 | 145 | a:='This is the text to encrypt/decrypt.'; | |
| 147 | 146 | writeln(a); | |
| 148 | 147 | writeln('Encrypting..'); | |
| 149 | 148 | a:=Crypt(a, 12345); | |
| 150 | 149 | writeln(a); | |
| 151 | 150 | writeln('Decrypting..'); | |
| 152 | 151 | a:=Crypt(a, 12345); | |
| 153 | 152 | writeln(a); | |
| 154 | 153 | end. | |
| 155 | 154 | ``` | |
| 156 | 155 | ||
| 157 | 156 | ### See also | |
| 158 | 157 | ||
| 159 | 158 | 1. [LCG on wikipedia](https://en.wikipedia.org/wiki/Linear_congruential_generator) | |
| 160 | 159 | 2. [[Generating Random Numbers]] | |
| 161 | 160 | ||
| 162 | 161 | ## See also | |
| 163 | 162 | ||
| 164 | 163 | - [Wikipedia article on the linear congruential generator algorithm](http://en.wikipedia.org/wiki/Linear_congruential_generator) | |
| 165 | 164 | ||
| 166 | 165 | --- | |
| 167 | 166 | *From the Free Pascal wiki, page "Delphi compatible LCG Random", as it stood on 2024-05-14 after a change by Alextp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 168 | 167 | Category: Modelling and Simulation | |
| 169 | 168 | Category: Statistical algorithms |
