* * *

Author Topic: Randomize improvement  (Read 383 times)

Eugene Loza

  • Sr. Member
  • ****
  • Posts: 326
    • Decoherence Studio :)
Randomize improvement
« on: December 07, 2016, 10:19:17 am »
Recently I've been toying with xorshift random generation algorithm and to make it thread-safe and more random I've made a randomization procedure wihch together with gettickcount64 (as SysUtils does) considers also "now" and local variable memory address.
https://github.com/castle-engine/castle-engine/blob/master/src/base/castlerandom.pas#L96
Maybe SysUtils.randomize could be improved in a similar way?
(+) More random "random seed".
(+) Thread safety (as SysUtils.random is not intended to have multiple instances this is actually not needed)
(+) Randomize now doesn't have to be called only once (a problem sometimes encountered by people).
(-) This procedure is more complex and works significantly slower.

Also there is a urandom randomization algorithm http://wiki.freepascal.org/Dev_random which is even better. Are there any problems with using it as a default in *nix systems?
My games in Lazarus/CastleGameEngine:
Project Helena (TBS/RPG) http://sourceforge.net/projects/projecthelena/ (Alpha)
Fire Madness (bullet hell) https://github.com/eugeneloza/FireMadness/ (release candidate)
Decoherence: beyond Wizardry (RPG) https://github.com/eugeneloza/Mazer (just started)

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 552
Re: Randomize improvement
« Reply #1 on: December 07, 2016, 11:57:02 am »
Small side remark: random is in the system unit, not in sysutils.

Random must behave the same on all platforms, because it is often used to generate a reproducible sequence of numbers (by setting randseed to a particular value), and those should be the same everywhere.

You can call randomize as much as you want with system.random, it just decreases the amount of randomness you will get. There is no point in "fixing" that, since the whole point is that simply calling random over and over again will give you the maximum amount of randomness possible.

Thread-safety is indeed out of scope for the default random number generator, and if you need that, you should use a different one.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus