   Need Very Small Random Numbers
  My question is rather simple.  I need to know how to generate VERY VERY small random numbers in C++.  They will be double's.  I am sampling from a space ranging from a scale of 10^-1 in probability to a scale 10^-67 in probability.  For example, the probability of one sample may be 40.1% while the next might be (1.2*10^-49)%.  This is being used in a robotics project for monte carlo localization, so every generated probability must be taken into account regardless of how small it may be.  I will be sampling with replacement and obtaining a large number of samples.  When I try generating random doubles using something similar to:double X = XMin + rand() * (XMax - XMin) / RAND_MAX;It seems that the smallest generated number is never anywhere near the specified min. (usually smallest I get back is around 10^-3).If there is no easy/clean way to do this, I suppose I could multiply by 10 * log(min prob).  Then I would have the same problem only with whole numbers (need random number between 1 and 10^67).  Does anyone have any ideas on how to handle this.  I feel like I am missing something very obvious but can't quite put my finger on it!
  Have you looked at the rngs that come with the C++ standard library?  There are several generators and probability distributions to choose from.  Perhaps one of them (exponential comes to mind) will do the job for you.Look in <tr1/random> for the tr1 library and <random> for the C++0x library (they are different).   ;  Perhaps RAND_MAX is too small for your purposes. You can use a 64-bit PRNG or you can manufacture one by concatenating bits from several calls to rand(). You can then use the same type of formula you had before.You shouldn't worry that your event with probability 10^-49 will never be picked: Even if you did the absolutely correct thing, it wouldn't ever be picked in practice.   ;  Are these probabilities coming up when generating the candidate state, or when determining whether to accept the transition? In the latter case don't worry about it, beyond getting a PRNG of at least 32 bits. In the former case, it depends on your sample space.   ;  You aren't really trying to get small numbers; you're trying to get a high number of significant figures. Otherwise you could just divide the numbers you get by some large constant, but it does you no good if the range is 0..10^-30 since then all the 1% probability events always happen ;)You can synthesize the probability by just taking several RNG results in a row. Say for example that you get 16 random bits per call, and 2^-256 is the smallest probability you have to worry about. So you express every probability as X/2^256, except you store X as a series of 16 16-bit values - the first gives the most significant bits in the binary representation of X, etc. To check if the event occurred, you pull out an RNG result and compare it to the first number; if it's exactly equal, then you try the second etc. until you get something either higher (or equal, on the last one - reject) or lower (accept).   ;  Will this work?result = 1.0 / static_cast<double>(rand()) * static_cast<double>(rand())?Of course need to check to be sure the denominator is not zero.   ;  That won't give anything near a uniform distribution.   ;  Edit: Just realised you're not actually looking for a linear distribution. I kept getting numbers that were always much higher than the min in terms of exponent, but this is intentional. There should be a distribution function that can produce an even distribution when graphed on a logarithmic scale, which is what you need.Try this:std::tr1::uniform_real<double> dist(-67,-1);std::tr1::minstd_rand engine;double values[1000];for(int i=0; i<1000; i++)values = pow(10,dist(engine));[Edited by - taz0010 on November 18, 2010 12:45:26 AM];Quote:Original post by taz0010Edit: Just realised you're not actually looking for a linear distribution.I don't see why you think this. The purpose is to take an event probability P, and simulate an opportunity for the event to happen, and see whether it actually happens or not, right? We do this, conceptually, by generating a uniform distribution on [0..1) and seeing if it is less than P.;Quote:I don't see why you think this. The purpose is to take an event probability P, and simulate an opportunity for the event to happen, and see whether it actually happens or not, right? We do this, conceptually, by generating a uniform distribution on [0..1) and seeing if it is less than P.Oh, if that's what he's after then he can use the tr1::bernoulli_distribution generator.http://www.johndcook.com/cpp_TR1_random.html#bernoulliUsing rand() won't work with the probabilities he's specified because RAND_MAX is (usually) 32767. With only 32768 different possibilities, chances are something with a probability of say, 1/1000000 will never be picked.   