PDA

View Full Version : Random Number Stream Generator



Chambers
12-15-2006, 09:15 AM
OK, I've finished a class for a random number stream generator :)

Features are:
- You may instantiate multiple number streams
- Repeatability, based on 32bit int number seeds
- the PRNG is a Mersenne Twist algorithm, yielding a good distribution
- Various random functions:
-- rand() returns a 32bit int between 0 and 0xffffffff
-- normalized() returns a float between 0.0 and 1.0
-- even() returns bool (either true or false)
-- deviated returns, well, potentially ANY number, with the output being distributed in a bell curve based on a median and standard deviation that you supply the function.

If desired, I will implement state saving / loading features, though I don't know if this is necessary.

I don't foresee speed being an issue; on my machine (an Athlon XP 1700+) I can easily generate a few million deviated random numbers (they take far longer than the other varieties) in a second or two.


If people are OK with this, I'd like to incorporate it into ClanLib, as I believe such a system is rather useful. I'll have a patch ready for approval in a day or two.

...Chambers

PS In the archive, you will find 3 files: a header file, an implementation file and a test file. The test takes some user input, and validates the PRNG distributing results correctly.

Chambers
12-15-2006, 09:33 AM
Of course I forgot one...
ranged() will return a number distributed between the supplied minimum and maximum range values ;)

Chambers
12-18-2006, 10:37 PM
And of course, after more testing, I find that the deviated function doesn't distribute correctly... so, ignore it, please, until I find a better method :)

Chambers
12-20-2006, 08:31 AM
OK, I fixed the "deviated" function... kind of :) How I wanted it wasn't working, so I rewrote it and called it "distributed." The new functionality is simple:

50% of all data points will lie withing 1 deviation of the mean.
50% of all remaining data points (25% of the total) will lie within 1 additional deviation, and so forth.

A few intervals, and the probability that a data point will lie within them:
d=1, p=0.5
d=2, p=0.75
d=3, p=0.875
d=4, p=0.9375
...

It still gives the desired effect that the majority will lie within a certain area, yet any value is theoretically possible (though unlikely).

Seth
12-20-2006, 11:48 AM
Very nice! I'm not sure if CL really needs this internally or if it should stay a 'contribution/addon', maybe some of the other CL guys can weight in on that.

But in any case thanks for sharing it.

Chambers
12-20-2006, 07:23 PM
Well, I'll admit that I've been spoiled by the random stream feature of POV-Ray - which was actually my main motivation for implementing this ;)

Unique random streams are something that, when you haven't used them, don't seem terribly useful. But, once you're used to having them around, you can't imagine going back to a single stream :)

Chambers
06-15-2007, 06:04 AM
I've posted it again as a separate lib on my webpage (www.pacificwebguy.com), after a few more additions.

Namely, dice throws (single and multiple dice) {took about 5 minutes to add}, but also making it thread-safe :) {took about 1/2 hour to add}

I've also updated the testing example with it.

I know some other people were asking about threading; this might serve as a useful example.