double unifRandb()// Generate a random number between 0 and 1
{
return rand() / double(RAND_MAX);// return a uniform number in [0,1].
}
I've been using this code to generate a random number between 0 and 1. It has been working good for my purposes, but now I am needing to calculate the age of a hypothetical astroparticle called "Gravitars". Their age range is the same age of the milky way galaxy ~13.2 billion years to now. The random number generator I have does not generate very small numbers (<.01) due to the limited size of RAND_MAX, and since I am just generating a number between 0 -> 1, this causes problems when I am offsetting the randomly generated number by 13200000000 years. The smallest age's I am getting is 7 million years old, and the largest I am getting is 1.4 billion. Both number which are far from being a uniformally distributed age over a period of 13.2 billion years. Advice?
*Edit I've tried to use Mersenne Twister and such, but every time I load the header files (I am using Microsoft Visual C++), I get weird errors every time I try to use it, so I've had to do this with only the standard rand function. If there is a way to do it with just the standard library, that would be great!
I don't think there's a function in the standard library for random numbers other than rand(). You could make your own random number generator using the standard library though. I think it would be pretty easy if you know a little math and can make classes.