Interesting Note on Delphi 10.3 Random Call

I often deliberately do not call Randomize when in Debug mode. I then expect that running the program with exactly the same actions would produce the same consistent results for a number of Random(X) calls.

Not so when running Delphi 10.3 Sydney because a call to IOUtils.Tpath.Create before Application.Initialize does the Randomize. Each program run then returns different values.

I thought it was an issue with only Delphi 10.3 but my initial quick testing with Delphi 10.1, Delphi 10.2 and Delphi 11 were in error. I now believe they also call IOUtils.Tpath.Create.

class constructor TPath.Create;
begin
  Randomize;

Hi @RogerConnell ,

I thought I would go off on another tangent to your comment. I saw @vincent just wrote a solution for a somewhat similar problem with debugging for Time. See GitHub - VSoftTechnologies/VSoft.System.TimeProvider: A simple abstraction over Now/NowUTC for Delphi to make unit testing date based code easier. .

I then came across GitHub - freeonterminate/RandomManager: Random Algorithm Selector which allows you to select the random number generation algorithm.

1 Like

Thanks Geoff

“Fixing” this “Problem” was not a priority I just noticed unexpected results.

Your info led me to add a simple statement in the project source.

begin
  Application.Initialize;
{$ifDef Debug}
  System.RandSeed:=0;
{$EndIf}
  Application.CreateForm(TTFormFMXImageDemo, TFormFMXImageDemo);
  Application.Run;
end.

Which restores my expectations

4 Likes