The MASM Forum Archive 2004 to 2012

General Forums => The Laboratory => Topic started by: MichaelW on April 06, 2008, 06:16:10 PM

Title: Assembly-time random number generator
Post by: MichaelW on April 06, 2008, 06:16:10 PM
The attachment is a test of an assembly-time random number generator. I intended this to be used for timing code, where the runtime overhead of generating a random number or reading one from a table would be unacceptable. The current version is based on a generator by George Marsaglia that "concatenates two 16-bit multiply-with-carry generators, x(n)=36969x(n-1)+carry, y(n)=18000y(n-1)+carry mod 2^16, has period about 2^60 and seems to pass all tests of randomness" , available  here (http://www.ciphersbyritter.com/NEWS4/RANDC.HTM#369B5E30.65A55FD1@stat.fsu.edu). I originally selected the CONG generator because it was easier to implement, but for a base value of 2 it produced a repeating sequence, making it useless for generating a random sequence of binary digits.

[attachment deleted by admin]
Title: Re: Assembly-time random number generator
Post by: drizz on April 08, 2008, 06:05:00 AM
Three years ago i also needed the ability to generate random data on every program compile/build on assembly time.
This is what i came up with by reading Knuth's book, it worked for me:
@Random macro Range
; Xn = (aXn-1 + b) mod m
@RandSeed = ((@RandSeed*54321)+12345) MOD 31337
exitm %@CatStr(%(@RandSeed mod (Range)))
endm

I only needed to generate random bytes.

And this is the cool initialization of seed i used :)
@RandSeed = @CatStr(@SubStr(<%@Time>,1,2),@SubStr(<%@Time>,4,2),@SubStr(<%@Time>,7,2))

By no means did i post this to compete with the tested RNG you chose, just to hint the initialization of seed with @Time.
Title: Re: Assembly-time random number generator
Post by: MichaelW on April 08, 2008, 06:23:57 AM
Thanks, that is a good idea, but I think there should still be a default seed because sometimes you need the ability to start at a fixed location in the sequence.
Title: Re: Assembly-time random number generator
Post by: MazeGen on April 10, 2008, 04:36:31 PM
Not always handy, sometimes useful. Using /D option under XP (NT?) and higher:

/DRANDOM=%RANDOM%
Title: Re: Assembly-time random number generator
Post by: daydreamer on April 10, 2008, 05:36:19 PM
perlin noisegenerating data would be not only cool, but must be lot safer and easier to debug to get it to look right than to shot myself in the foot every once in a while
especially as for a 256x256 static data you will anyway need to wait for it when using masm, you could as well fool masm todo something more useful than create a buffer of static data