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,509 other followers

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 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 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.

    http://delphiscience.wordpress.com/2012/10/15/understanding-the-nature-of-randomness-and-testing-delphis-random-function/

    But that doesn’t make it a CSPRNG.

    • David Heffernan said

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

  3. 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 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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: