Thread: more on random numbers...

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    more on random numbers...

    Consider the following functions for generating random numbers:
    Code:
    double uniform_deviate( int seed ){
    	
    	return seed * ( 1.0 / ( RAND_MAX + 1.0 ) );
    }
    
    
    
    
    int get_rand( int UPPER_BOUND ){
    	
    	return static_cast<int>( uniform_deviate( rand() ) * UPPER_BOUND );
    }
    
    
    
    
    int get_rand( int LOWER_BOUND, int UPPER_BOUND ){
    	
    	return static_cast<int>( LOWER_BOUND + uniform_deviate( rand() ) * ( UPPER_BOUND - LOWER_BOUND ) );
    }
    I'd like to write the following function get_rand() with no arguments:
    Code:
    int get_rand(){
    
        // ...
    }
    To use in this normal distribution function:
    Code:
    double normal(const double &mean, const double &sdiv){
    
      static const double PI = 3.1415927;
      static const double R_MAX = RAND_MAX + 1;
    
      return sdiv * sqrt( -2 * log( ( rand() + 1 )/R_MAX) ) * sin( 2 * PI * rand()/R_MAX ) + mean;
    
    }
    My question is how do implement get_rand() with no arguments to replace the calls to rand() in the normal() function? Is it even necessary?

    Thoughts?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What's wrong with
    Code:
    int get_rand(){
    	
    	return static_cast<int>( uniform_deviate( rand() ) * R_MAX );
    }
    where R_MAX is whatever you think it is in your normal() function?

  3. #3
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    thanks.

    Now what's the deal with:
    Code:
    static const double R_MAX = RAND_MAX + 1;
    Is THAT necessary?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just makes it easier to read than some magic numbers...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dudeomanodude View Post
    thanks.

    Now what's the deal with:
    Code:
    static const double R_MAX = RAND_MAX + 1;
    Is THAT necessary?
    Who knows what you mean by necessary? Since the uniform deviate function is supposed to give values in the range [0..1), we need to make sure 1 is not included, which is why we have the division by one more than the maximum output of rand(). I have no idea whether you're still using rand() somewhere or not.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    RAND_MAX + 1
    who said that this will not wrap around INT_MAX in case when RAND_MAX == INT_MAX ?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    who said that this will not wrap around INT_MAX in case when RAND_MAX == INT_MAX ?
    Just change it to RAND_MAX + 1.0
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM