Difference between revisions of "Seedable PRNG"
Jump to navigation
Jump to search
Gigs Taggart (talk | contribs) (update on periodicity) |
Huney Jewell (talk | contribs) m (Avoid line overflow) |
||
Line 1: | Line 1: | ||
{{LSL Header}} | |||
== md5 based seedable PRNG == | |||
It's not the fastest thing but it doesn't seem to have much if any output skew, and I couldn't detect any periodicity in the first 100,000 cycles. | It's not the fastest thing but it doesn't seem to have much if any output skew, and I couldn't detect any periodicity in the first 100,000 cycles. | ||
Line 19: | Line 22: | ||
integer prng_get() | integer prng_get() | ||
{ | { | ||
gLastHash=llMD5String(gLastHash, gSeed); | gLastHash=llMD5String(gLastHash, gSeed); | ||
//the bitwise thingy gets rid of negative numbers | |||
return (integer)("0x" + llGetSubString(gLastHash, 0, 7)) & 0x7FFFFFFF; } | |||
</pre> | </pre> | ||
[[Category: LSL Examples]] | [[Category: LSL Examples]] |
Revision as of 05:52, 7 September 2007
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
md5 based seedable PRNG
It's not the fastest thing but it doesn't seem to have much if any output skew, and I couldn't detect any periodicity in the first 100,000 cycles.
//new md5 based seedable PRNG //By: Gigs Taggart //Released under BSD License. //http://www.opensource.org/licenses/bsd-license.php integer gSeed=0; string gLastHash; prng_seed(integer seed) { gSeed=seed; } integer prng_get() { gLastHash=llMD5String(gLastHash, gSeed); //the bitwise thingy gets rid of negative numbers return (integer)("0x" + llGetSubString(gLastHash, 0, 7)) & 0x7FFFFFFF; }