Difference between revisions of "Pseudo-random Number Generator"

From Second Life Wiki
Jump to navigation Jump to search
 
Line 1: Line 1:
Heres a Pseudo-random Number Generator - I just made it up off the top of my head - so it has no mathematial research behind it to prove it's random... I've tested it for a while and it looks random to me, about the same as llFrand(). But what ever. Sue me.
Heres a Pseudo-random Number Generator - I just made it up off the top of my head - so it has no mathematical research behind it to prove it's random... I've tested it for a while and it looks random to me, about the same as llFrand(). But what ever. Sue me.


It's decently fast (haven't compared it with llFrand()) - yet - but I would expect it to be about the same speed, if not faster.
I use this for determining a random channel for two (or more) objects to talk on. This allows the scripts to talk without users being able to intercept the messages - and even if they do - then the channel will change in a minute or two - so no harm done.
 
I recently compared this with llFrand() and it is on average 5% faster.




<pre>
<pre>
// IMPORTANT: Change the following number before using!
integer seed = 0x61FA687C;
integer seed = 0x61FA687C;



Revision as of 11:34, 23 September 2007

Heres a Pseudo-random Number Generator - I just made it up off the top of my head - so it has no mathematical research behind it to prove it's random... I've tested it for a while and it looks random to me, about the same as llFrand(). But what ever. Sue me.

I use this for determining a random channel for two (or more) objects to talk on. This allows the scripts to talk without users being able to intercept the messages - and even if they do - then the channel will change in a minute or two - so no harm done.

I recently compared this with llFrand() and it is on average 5% faster.


// IMPORTANT: Change the following number before using!
integer seed = 0x61FA687C;


integer rand() {
    seed = (integer)(seed * 0x71B5F252 + 0xD);
    return seed ^ 0x7FFFFFFF;
}

Example code to test the randomness (Is that a word?) of the generator.

default {
	state_entry() {
		integer i = 0;
		integer min = 0x7FFFFFFF;
		integer max = -0x7FFFFFFF;
		integer total = 0;
		while (i < 100000000) {
			integer r = rand();
			if (r < min) {
				min = r;
			}
			if (r > max) {
				max = r;
			}
			total += r;
			i++;
		}
		llOwnerSay("Min: " + (string)min);
		llOwnerSay("Max: " + (string)max);
		llOwnerSay("Average: " + (string)(total/100000000));

	}

}