
Rob Kendrick wrote:
On Sun, 17 Jan 2010 20:59:41 +0100 Wolfgang Pupp <wolfgang.pupp@gmail.com> wrote:Btw., is there some decent, "resumable" RNG for Lua around? What I want is essentially a "getrandomseed", which returns the current seed and can thus be used for resuming the generation of random numbers later, with perfectly predictable output.You can do this with a cryptographic hash. For each "number" you want out, hash together your seed and an index. Should you ever want the same index again, just hash the seed and index again. Want the next number in the sequence? Increase the index. Etc. Should be easy to implement this in Lua using an offtheshelf hash library. (like lmd5). B.
A neat trick indeed, and can be handy to get a repeatable random number stream from such things as simulation in games.
If one has access to a good Lua bit lib, one can use a MD5 hash derivative for better shortterm distribution of result values. Just split the 128 bit hash into multiple groups of bits (4 of 32?), combine them somehow (XOR?), and interpret the result as an xx (32 in this case) bit integer. This would be made much easier and efficient with the inclusion of binary string bit operations, of course.
(the only time I would use a bit library in Lua is with binary strings, excluding a few exceptions  but maybe it is just me. I throw that thought into the current bitlib discussion)
Here is some light reading on the subject: http://www.mawhorter.net/webdevelopment/repeatablerandomanevendistributionofpseudorandomnessHe used SHA256 and summed the byte value of all the ASCII hex characters, but the idea is the same.