# Thread: more on random numbers...

1. ## 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?

2. 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. thanks.

Now what's the deal with:
Code:
`static const double R_MAX = RAND_MAX + 1;`
Is THAT necessary?

4. Just makes it easier to read than some magic numbers...

5. Originally Posted by dudeomanodude
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. RAND_MAX + 1
who said that this will not wrap around INT_MAX in case when RAND_MAX == INT_MAX ?

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