Hi all, I am currently using boost::random, and I wonder how it is possible to get uniform random numbers on a union of two intervalls. For instance, I would like to generate uniform_real numbers in the intervall [-a;-b]U[b;a]. How can I do such a task ? Best regards, Olivier Tournaire
On 21 Jul 2008, at 16:47, Olivier Tournaire wrote:
I am currently using boost::random, and I wonder how it is possible to get uniform random numbers on a union of two intervalls. For instance, I would like to generate uniform_real numbers in the intervall [-a;-b]U[b;a].
Generate a number uniformally in the interval [0,2*(a-b)]. If it is smaller than a-b then add b to it. If it is larger than a-b then multiply it by -1. I think any function which picks the two intervals with equal probability should work. Thanks, Kevin Martin
If b is small compared to a, generate U[-a,a] and reject values in (-b,b) -- that is, generate a new value when that happens. This is the general solution to the problem of generating a random variate from an oddball distribution: you generate from a distribution that dominates yours and reject values you don't want. When the PDF of your desired distribution lies between zero and the envelope distribution you need another random variate to act as a surrogate: if U[0,envelope-value(x)] < [my-funky-dist-value(x)] we keep the first variate x, else we generate a new pair.
For instance, I would like to generate uniform_real numbers in the intervall [-a;-b]U[b;a].
The previous answer is an example of a less general but often more efficient approach: cut up another distribution and paste it back together to make up yours. These approaches can be combines: paste up something close and use surrogate screening to fine-tune. See any good book on simulation. -swn
Thanks Kevin and Stephen for your answers.
2008/7/21 Stephen Nuchia
See any good book on simulation.
Do you have any reference ? Regards
-swn
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Knuth discusses the rejection technique in section 3.4.1 (2nd ed, vol. 2), in the context of generating normal RVs from uniform. His discussion touches on the related issues of long tails and ways to (usually) avoid evaluating the density function. He also points out that the most general mechanism employs the inverse of the distribution (not density) function, though in practice I've not seen it used much except with empirical PDFs (using sorted data as an interpolation table) and for the few standard infinite-support distributions that have closed-form inverses. I was lax with notation in my original response: PDF conventionally denotes the (cumulative) distribution and pdf the density (derivative of the distribution). Because the pdf is what usually gets used day-to-day one begins to forget this after leaving school. -swn
participants (3)
-
Kevin Martin
-
Olivier Tournaire
-
Stephen Nuchia