Recent

Author Topic: [SOLVED] Does Lazarus use same random number generator as Delphi 3 Standard?  (Read 1676 times)

AMJF

  • New Member
  • *
  • Posts: 48
I'll get straight to the point: I have an old app I wrote back in the day on Borland Delphi 3, which featured the ability to use registration codes. I wrote a generator for those registration codes, but I have long since lost it. I've got the source code for unlocking the codes, but it uses random numbers to generate it. I thought of writing a program to get a code quickly, but I doubt Lazarus uses the same random number generator as Delphi 3 did. Also, I can't just run Delphi 3 and use it that way as Windows 10 64-bit doesn't support it, at least the installer.

Before you think I'm engaging in anything dodgy, please be aware I am the author of the program, and I am just trying to get my own key without suffering the nag screen every 10 seconds.

Here's my source code for the registration checker in question, it was over 16 years ago that I last updated the code and has no real commercial value today anyway:

Code: Pascal  [Select][+][-]
  1. // ==================
  2. // AboutApp v2.0
  3. // By Aaron MJ Fisher
  4. // Copyright © 2007
  5. // ------------------
  6. // Source Code
  7. // ==================
  8.  
  9. unit AA;
  10.  
  11. interface
  12.  
  13. uses
  14.   Windows, SysUtils, Forms, Registry, ExtCtrls, StdCtrls, Controls, Classes, ComCtrls;
  15.  
  16. type
  17.   TfAA = class(TForm)
  18.     abAbout    : TBevel;
  19.     aiAppIcon  : TImage;
  20.     sbOK       : TButton;
  21.     seKey      : TEdit;
  22.     slAppTitle : TLabel;
  23.     slAppVers  : TLabel;
  24.     slAppInfo1 : TLabel;
  25.     slAppInfo2 : TLabel;
  26.     slAppType  : TLabel;
  27.     slKey      : TLabel;
  28.     procedure FormActivate(Sender:TObject);
  29.     procedure FormCreate(Sender:TObject);
  30.     procedure sbOKClick(Sender:TObject);
  31.   public
  32.     // ABOUTAPP SYSTEM
  33.     // System
  34.     procedure AASetup;
  35.     procedure AAUserOpen;
  36.     procedure AAUserSave;
  37.     procedure AAUserCheck;
  38.   end;
  39.  
  40. const
  41.   // Application Info
  42.   cAPPTITLE = 'TypeStats';
  43.   cAPPVER   = '3.11';
  44.   cAPPINFO1 = 'By Aaron MJ Fisher';
  45.   cAPPINFO2 = 'Copyright © 1998-2007';
  46.   cAPPTYPE  : Array [0..1] of String = ('Shareware','Registered');
  47.  
  48.   // KeyGen Info
  49.   APPCODE = 3753;
  50.   APPSEED = 1926;
  51.  
  52.   // Windows Registry Settings
  53.   cRROOTKEY = HKEY_CURRENT_USER;
  54.   cAARSPATH = 'Software\'+cAPPTITLE+'\User';
  55.  
  56. var
  57.   fAA : TfAA;
  58.  
  59.   AARGY : TRegistry;
  60.  
  61. implementation
  62.  
  63. {$R *.DFM}
  64.  
  65.  
  66. // ===============
  67. // ABOUTAPP SYSTEM
  68. // ===============
  69.  
  70. // ======
  71. // System
  72. // ======
  73.  
  74. // ====================
  75. procedure TfAA.AASetup;
  76. // ====================
  77. begin
  78.   // DISPLAY ABOUT TABSHEET INFO
  79.   // Application Info
  80.   Caption:='About '+cAPPTITLE;
  81.   aiAppIcon.Picture.Graphic:=Application.Icon;
  82.   slAppTitle.Caption:=cAPPTITLE;
  83.   slAppVers.Caption:='Version '+cAPPVER;
  84.   slAppInfo1.Caption:=cAPPINFO1;
  85.   slAppInfo2.Caption:=cAPPINFO2;
  86.   slAppType.Caption:=cAPPTYPE[0];
  87.  
  88.   // OPEN USER SETTINGS
  89.   AAUserOpen;
  90. end;
  91.  
  92. // =======================
  93. procedure TfAA.AAUserOpen;
  94. // =======================
  95. begin
  96.   // READ USER SETTINGS FOR APP FROM REGISTRY
  97.   AARGY:=TRegistry.Create;
  98.   try
  99.     AARGY.Rootkey:=cRROOTKEY;
  100.     if AARGY.OpenKey(cAARSPATH,False) then begin
  101.       seKey.Text:=Trim(AARGY.ReadString('Key'));
  102.       AARGY.CloseKey;
  103.     end;
  104.   finally
  105.     AARGY.Free;
  106.   end;
  107.  
  108.   // CHECK USER SETTINGS
  109.   AAUserCheck;
  110. end;
  111.  
  112. // =======================
  113. procedure TfAA.AAUserSave;
  114. // =======================
  115. begin
  116.   // WRITE USER SETTINGS FOR APP TO REGISTRY
  117.   AARGY:=TRegistry.Create;
  118.   try
  119.     AARGY.Rootkey:=cRROOTKEY;
  120.     AARGY.OpenKey(cAARSPATH,True);
  121.     AARGY.WriteString('Key',Trim(seKey.Text));
  122.     AARGY.CloseKey;
  123.   finally
  124.     AARGY.Free;
  125.   end;
  126.  
  127.   // CHECK USER SETTINGS
  128.   AAUserCheck;
  129. end;
  130.  
  131. // ========================
  132. procedure TfAA.AAUserCheck;
  133. // ========================
  134. var
  135.   SETKEY,CMPKEY : String;
  136.   T             : Integer;
  137.   KEYPASS       : Boolean;
  138. begin
  139.   // COMPARE KEY WITH LIST
  140.   KEYPASS:=False;
  141.   RandSeed:=APPSEED;
  142.   SETKEY:=Trim(seKey.Text);
  143.   for T:=0 to 9999 do begin
  144.     CMPKEY:=IntToStr(APPCODE)+'-'+IntToStr(1000+T)+'-'+IntToStr(1000+Random(8999));
  145.     if SETKEY=CMPKEY then KEYPASS:=True;
  146.   end;
  147.   // Display application type
  148.   if KEYPASS then
  149.     slAppType.Caption:=cAPPTYPE[1]
  150.   else
  151.     slAppType.Caption:=cAPPTYPE[0];
  152. end;
  153.  
  154.  
  155. procedure TfAA.FormActivate; begin AAUserOpen; end;
  156. procedure TfAA.FormCreate; begin AASetup; end;
  157.  
  158. procedure TfAA.sbOKClick; begin AAUserSave; close; end;
  159.  
  160.  
  161. end.
  162.  
« Last Edit: February 21, 2023, 09:53:02 pm by AMJF »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11448
  • FPC developer.
Re: Does Lazarus use same random number generator as Delphi 3 Standard?
« Reply #1 on: February 21, 2023, 09:36:28 am »
Afaik no, FPC uses a Mersenne Twister for better randomness.

So the only logical solution seems to dig up source for the D3 one, and try to transform it into an unit and get it to run. I hope it is not assembler.

Stefan Glienke

  • New Member
  • *
  • Posts: 23
Re: Does Lazarus use same random number generator as Delphi 3 Standard?
« Reply #2 on: February 21, 2023, 11:01:38 am »
I can't just run Delphi 3 and use it that way as Windows 10 64-bit doesn't support it, at least the installer.
Use a VM.

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Does Lazarus use same random number generator as Delphi 3 Standard?
« Reply #3 on: February 21, 2023, 12:44:24 pm »
There is all on the wiki: https://en.wikipedia.org/wiki/Linear_congruential_generator, parameters of generator and pascal code. They say Delphi, without version, so maybe all versions use the same random generator?
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

AMJF

  • New Member
  • *
  • Posts: 48
Re: Does Lazarus use same random number generator as Delphi 3 Standard?
« Reply #4 on: February 21, 2023, 02:53:34 pm »
I can't just run Delphi 3 and use it that way as Windows 10 64-bit doesn't support it, at least the installer.
Use a VM.

I'd have to have a copy of the OS required, wouldn't I? I don't tend to keep old versions of Windows once I upgrade.

Thaddy

  • Hero Member
  • *****
  • Posts: 14368
  • Sensorship about opinions does not belong here.
Re: Does Lazarus use same random number generator as Delphi 3 Standard?
« Reply #5 on: February 21, 2023, 03:41:02 pm »
I have written a Delphi 3 compatible one some years ago and it is in the wiki.
https://wiki.freepascal.org/Delphi_compatible_LCG_Random

It generates the exact same sequence.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14368
  • Sensorship about opinions does not belong here.
Re: Does Lazarus use same random number generator as Delphi 3 Standard?
« Reply #6 on: February 21, 2023, 03:42:30 pm »
Afaik no, FPC uses a Mersenne Twister for better randomness.

So the only logical solution seems to dig up source for the D3 one, and try to transform it into an unit and get it to run. I hope it is not assembler.

Not anymore, the generator has changed in trunk. https://wiki.freepascal.org/User_Changes_Trunk#Random_generator
Also, see above for Delphi compatibility.
« Last Edit: February 21, 2023, 04:01:52 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14368
  • Sensorship about opinions does not belong here.
Re: Does Lazarus use same random number generator as Delphi 3 Standard?
« Reply #7 on: February 21, 2023, 03:44:02 pm »
Code is here too, and is completely clean-roomed (it always was):
Code: Pascal  [Select][+][-]
  1. unit lcg_random;
  2. // Delphi compatible LCG random number generator routines for Freepascal.
  3. // (c)2017, Thaddy de Koning. Use as you like
  4. // Algorithm, Delphi multiplier and increment taken from:
  5. // https://en.wikipedia.org/wiki/Linear_congruential_generator
  6. // The default Delphi RandomSeed is determined as zero.
  7. {$ifdef fpc}{$mode objfpc}{$endif}
  8.  
  9. interface
  10.  
  11. function LCGRandom: extended; overload;inline;
  12. function LCGRandom(const range:longint):longint;overload;inline;
  13.  
  14. implementation
  15.  
  16. function IM:cardinal;inline;
  17. begin
  18.   RandSeed := RandSeed * 134775813  + 1;
  19.   Result := RandSeed;
  20. end;
  21.  
  22. function LCGRandom: extended; overload;inline;
  23. begin
  24.   Result := IM * 2.32830643653870e-10;
  25. end;
  26.  
  27. function LCGRandom(const range:longint):longint;overload;inline;
  28. begin
  29.   Result := IM * range shr 32;
  30. end;
  31.  
  32. end.
Examples in the wiki above. Works from D2 up until 10.4. Since it is short, here is the example:
Code: Pascal  [Select][+][-]
  1. program lcgdemo;
  2. // Compile and run in both Delphi and FPC and compare the output.
  3. // It is exactly the same
  4. {$ifdef fpc}{$mode objfpc}
  5. {$Macro on}{$define random := LCGRandom}
  6. {$endif}
  7. {$ifdef mswindows}{$apptype console}{$endif}
  8. uses lcg_random;
  9.  
  10. var i:integer;
  11. begin
  12.   RandSeed := 999;  // default delphi randseed is zero
  13.   for i := 1 to 20 do
  14.   begin
  15.     write(Random(100):4); // Delphi: Random FPC:LCGRandom. See macro
  16.     if i mod 5 = 0 then writeln;
  17.   end;
  18.  
  19.   RandSeed := 999;  // default delphi randseed is zero
  20.   for i := 1 to 20 do
  21.   begin
  22.     write(Random:4:8,' ');// Delphi: Random FPC:LCGRandom
  23.     if i mod 5 = 0 then writeln;
  24.   end;
  25.   Readln;
  26. end.

The example is also the proof that the generated values are the same in both FreePascal and Delphi (3).

Simply ignore simply most of the other answers but not mine. This one is correct.
I wrote it for exactly the same reason as you: a series of PRNG values that need to be the same in Delphi and FPC.
« Last Edit: February 21, 2023, 04:03:00 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

AMJF

  • New Member
  • *
  • Posts: 48
Re: Does Lazarus use same random number generator as Delphi 3 Standard?
« Reply #8 on: February 21, 2023, 08:15:00 pm »
Thaddy, you're A LIFESAVER!

I combined the code you posted with the code I provided and it worked, my program is registered so the nag screen has disappeared!

I could provide the code I used to solve the problem, but the idea is that the program looked for any one of the random numbers generated by Delphi 3 as a valid registration code, so all I did was list the first 20 numbers as in the loop in my code, and any one of them could be used if I wanted to!

Thanks for this, Thaddy!

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Does Lazarus use same random number generator as Delphi 3 Standard?
« Reply #9 on: February 21, 2023, 08:32:31 pm »
Thaddy, I didn't know that the code on the wikipedia is from you. Nice.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Thaddy

  • Hero Member
  • *****
  • Posts: 14368
  • Sensorship about opinions does not belong here.
BTW: Since in trunk/main the PRNG has changed: I also did a - threadsafe - Freepascal compatible mersenne twister in 2017, which also in the wiki. Might come in handy if you have data that relies on the Mersenne Twister code. Current stable, still has the Mersenne twister, though.

What I find on this subject is what I also told the core developers at the time: basically never change your pseudo random generator, if only because of the subject that started this thread. But they would not listen, so I wrote them (both, and some more, from scratch :D ;D )

I needed it for some Monte Carlo simulations.
https://en.wikipedia.org/wiki/Monte_Carlo_method
« Last Edit: February 22, 2023, 12:20:39 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Curt Carpenter

  • Sr. Member
  • ****
  • Posts: 402
Great information -- which prompts me to ask:  Is there an index to all the articles on wiki.freepascal.org ?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Great information -- which prompts me to ask:  Is there an index to all the articles on wiki.freepascal.org ?

AllPages good enough for you? ;)

Curt Carpenter

  • Sr. Member
  • ****
  • Posts: 402
Wow.  Thank you!

 

TinyPortal © 2005-2018