Difference between revisions of "Seedable PRNG"

From Second Life Wiki
Jump to navigation Jump to search
(update on periodicity)
m (<lsl> tag to <source>)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{LSL Header}}
See also: [[llFrand]], [[llListRandomize]], [[Pseudo-random_Number_Generator]]
Note: The [[32bit_Hash]] example presents just the conversion to integer from llMD5String.
== 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.


<pre>
<source lang="lsl2">


//new md5 based seedable PRNG
//new md5 based seedable PRNG
Line 15: Line 22:
     gSeed=seed;
     gSeed=seed;
}
}


integer prng_get()
integer prng_get()
{
{
     gLastHash=llMD5String(gLastHash, gSeed);
     gLastHash=llMD5String(gLastHash, gSeed);  
     return (integer)("0x" + llGetSubString(gLastHash, 0, 7)) & 0x7FFFFFFF; //the bitwise thingy gets rid of negative numbers
    //the bitwise thingy gets rid of negative numbers
     return (integer)("0x" + llGetSubString(gLastHash, 0, 7)) & 0x7FFFFFFF;
}
}


</pre>
default
{
    state_entry()
    {
        prng_seed(2007031901);


[[Category: LSL Examples]]
        string line;
        integer index;
 
        line = "";
        for (index = 0; index < 9; ++index)
        {
          line += " " + (string) prng_get();
        }
        llOwnerSay(line);
 
        line = "";
        for (index = 0; index < 30; ++index)
        {
          line += " " + (string) (1 + (prng_get() % 6));
        }
        llOwnerSay(line);
 
        llOwnerSay("OK");
    }
}
</source>
[[Category: LSL Examples]] [[Category: LSL Encryption]]

Latest revision as of 16:54, 24 January 2015

See also: llFrand, llListRandomize, Pseudo-random_Number_Generator

Note: The 32bit_Hash example presents just the conversion to integer from llMD5String.

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;
}

default
{
    state_entry()
    {
        prng_seed(2007031901);

        string line;
        integer index;

        line = "";
        for (index = 0; index < 9; ++index)
        {
           line += " " + (string) prng_get();
        }
        llOwnerSay(line);

        line = "";
        for (index = 0; index < 30; ++index)
        {
           line += " " + (string) (1 + (prng_get() % 6));
        }
        llOwnerSay(line);

        llOwnerSay("OK");
    }
}