Results 1 to 7 of 7

Thread: Random Number Stream Generator

  1. #1
    Lesser Knight
    Join Date
    Sep 2006
    Posts
    41

    Default Random Number Stream Generator

    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.
    Attached Files Attached Files

  2. #2
    Lesser Knight
    Join Date
    Sep 2006
    Posts
    41

    Default

    Of course I forgot one...
    ranged() will return a number distributed between the supplied minimum and maximum range values

  3. #3
    Lesser Knight
    Join Date
    Sep 2006
    Posts
    41

    Default

    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

  4. #4
    Lesser Knight
    Join Date
    Sep 2006
    Posts
    41

    Default

    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).
    Attached Files Attached Files
    Last edited by Chambers; 12-20-2006 at 08:33 AM. Reason: Of course, I forgot to attach the file :eek:

  5. #5
    Administrator Seth's Avatar
    Join Date
    Jul 2002
    Location
    Japan
    Posts
    5,340

    Default

    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.
    Seth A. Robinson
    Robinson Technologies

  6. #6
    Lesser Knight
    Join Date
    Sep 2006
    Posts
    41

    Default

    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

  7. #7
    Lesser Knight
    Join Date
    Sep 2006
    Posts
    41

    Default

    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.

Similar Threads

  1. A couple of random queries
    By Dan in forum Funeral Quest
    Replies: 7
    Last Post: 06-30-2003, 09:33 AM

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •