Boost logo

Boost :

From: Eric Ford (eford_at_[hidden])
Date: 2001-10-01 14:50:09


Again, my first reply got lost. I'll try once more...

> I'm just look into the normal_distribution.hpp and see it's use sin
and
> cos for calculating the result (use the Box-Muller Method), altough
it
> can be "faster" with the Polar Method (see eg:
> http://www.dspguru.com/howto/tech/wgn2.htm)
> the strange thing that the current version are faster even it use
> trigonometric functions(?) at least on a PIII with MSVC 6sp4 when
> I compile release (optimezed) code.
> (I append the patch which I made for the test at the end of this
mail).

The relative speed on the two algorithsm depends upon the speed of the
RNG and the trig functions. The Mercenne Twister produces very good
RNs, but isn't the fastest. I'm guessing that if you used a fast RNG
(e.g. linear congruential), then the looping algorithm could be
faster. Also, modern computers are much better at things like sin and
cos than they used to be. I wouldn't be opposed to boost providing
both algorithms, using the current as the default.

> I can't find the reason. but another intersting question if the
> current implementation use the Box-Muller Method while don't have
> a do loop:
> do {
> _r2 = _rng();
> } while (_r2 == 0);
> and what's the reason of the
> _cached_rho = sqrt(-2 * log(1.0-_r2));
> and not the
> _cached_rho = sqrt(-2 * log(r2));
> ?

The RNs are [0,1), so 1-_r2 is (0,1]. Thus answering both your
questions.

E


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk