The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

Delphi: RandomizeIfNeeded

Posted by jpluimers on 2014/10/07

Calling Randomize too often can make your Random numbers even less random.

Sometimes having the Randomize call in a unit initialization section is not practical.

Hence this little method that I think I first wrote back in the Turbo Pascal days:

procedure RandomizeIfNeeded();
begin
  if RandSeed = 0 then 
    Randomize();
end;

–jeroen

7 Responses to “Delphi: RandomizeIfNeeded”

  1. Kevin G. McCoy's avatar

    Kevin G. McCoy said

    Just put Randomize in an initialization block somewhere. It does not matter how many times you call it on startup; It only matters if you call it repeatedly between generating random numbers. As others pointed out, you should use another algorithm if you want good quality random numbers. See Schneier’s Applied Cryptography book for some good ones.

    • jpluimers's avatar

      jpluimers said

      Indeed non both cases: Don’t call Randomize in between obtaining Random numbers, creating Delphi’s random number generator is certainly for from any cryptographically secure PRNG.

  2. @David: Check out this article where Delphi’s Random was compared to Random.org output. It actually did really good.

    Understanding the nature of Randomness and Testing Delphi’s Random function

    But that doesn’t make it a CSPRNG.

    • David Heffernan's avatar

      David Heffernan said

      @Jim That article is bogus. LCGs are well known to be poor. This is well documented.

  3. David Heffernan's avatar

    David Heffernan said

    Calling Randomize at start up is fine in principle, but you shouldn’t use Randomize or Random because they are rather hopeless. Very poor PRNG, and not threadsafe. It’s 2014.

  4. Klaus's avatar

    Klaus said

    Why should “having the Randomize call in a unit initialization section is not practical”?
    Thanks for futher enlightment!
    Regards, Klaus

    • I don’t think he is saying it isn’t practical as a rule, but just that there may be times it isn’t practical. Perhaps there are multiple units that want to call Randomize in the initialization section, although that probably won’t change the quality of the randomness.

Leave a reply to jpluimers Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.