more on random numbers...

This is a discussion on more on random numbers... within the C++ Programming forums, part of the General Programming Boards category; Consider the following functions for generating random numbers: Code: double uniform_deviate( int seed ){ return seed * ( 1.0 / ...

  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,185
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    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,185
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    RAND_MAX + 1
    who said that this will not wrap around INT_MAX in case when RAND_MAX == INT_MAX ?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,646
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 09:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 06: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, 09:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21