Thread: Random Numbers class wrapper (based on Julienne Walker's implementation)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    Random Numbers class wrapper (based on Julienne Walker's implementation)

    I'm wrapping Prelude's implementation of jsw_rand inside a class. So far this is what I have:

    CRand.h
    PHP Code:
    #ifndef CLASS_RANDOM_GENERATOR_H
    #define CLASS_RANDOM_GENERATOR_H

    /*
      Random number Class

        - Wrapped around Julienne Walker's implementation of Mersenne Twister's found at
          http://eternallyconfuzzled.com/brain.html, as of July, 18, 2006
        - Slightly altered to accomodate the class implementation
    */

    class CRand {
    private:
        static 
    unsigned int time_seed();

        static const 
    int N 624;
        static const 
    int M 397;
        static const 
    unsigned long A  0x9908b0dfUL;
        static const 
    unsigned long U  0x80000000UL;
        static const 
    unsigned long L  0x7fffffffUL;

    public:
        
    explicit CRand(unsigned longunsigned long);

        
    // TODO: void reseed(unsigned long = time_seed());
        // TODO: unsigned long rethrow();
    private:

        
    void seed(unsigned long time_seed());
       
    // TODO: unsigned long generate();

        
    unsigned long low_;
        
    unsigned long high_;
        
    unsigned long res_//holds random

        // internal state (per object)
        
    unsigned long x[N];
        
    int next;
    };

    #endif // CLASS_RANDOM_GENERATOR_H 
    CRand.cpp
    PHP Code:
    #include <climits>
    #include <ctime>
    #include "crand.h"

    CRand::CRand(unsigned long lowunsigned long high): low_(low), high_(high) {
        if(
    low_ high_) {
            
    low_ high;
            
    high_ low;
        }

        if( 
    low_ != high_ ) {
            
    seed();
            
    //TODO: res_ = generate();
        
    } else res_ low_;
    }

    unsigned int CRand::time_seed() {
      
    std::time_t now std::time(0);
      
    unsigned char *= (unsigned char *)&now;
      
    unsigned seed 0;
      
    std::size_t i;

      for (
    0sizeof nowi++)
        
    seed seed * ( UCHAR_MAX 2U ) + p[i];

      return 
    seed;
    }

    /* Initialize internal state */
    void CRand::seed(unsigned long s) {
      
    int i;

      
    x[0] = 0xffffffffUL;

      for ( 
    1Ni++ ) {
        
    x[i] = ( 1812433253UL
          
    * ( x[1] ^ ( x[1] >> 30 ) ) + );
        
    x[i] &= 0xffffffffUL;
      }

    I'm about to implement the actual jsw_rand() algorithm into the class. However, I'm afraid I couldn't fully understand the discussion about distribution found at http://eternallyconfuzzled.com/articles/rand.html.

    My question is, will it be ok, after using Prelude's algorithm jsw_rand(), to use the modulos operator to bring the resulting long into the range defined by my data members low_ and high_? Or should I instead use the uniform deviation she discusses?

    Basically... since I'm having an hard time interpreting the code inside jsw_rand() (and because math is not one of my best attributes) I'm at loss as to which it already provides a distribution safeguard(?). My guess is it doesn't and I should either call it repeatedly or provide a uniform deviation algorithm as she discusses.
    Last edited by Mario F.; 07-18-2006 at 08:05 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help generating random numbers in MFC
    By drb2k2 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:52 AM
  2. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  3. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  4. Replies: 3
    Last Post: 01-14-2002, 05:09 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM