4 ms·
Not necessarily the most efficient though: the Box-Muller transform that is probably used in the call to randn is related to the transform you'd need to start f
by carlob 12y ago
Not necessarily the most efficient though: the Box-Muller transform that is probably used in the call to randn is related to the transform you'd need to start from uniformly distributed reals.
So your solution ends up pretty much using the same math (while probably calling cos a few more times) and throwing away at least one random number and possibly half of them (depending on the implementation of randn).
http://en.wikipedia.org/wiki/Box–Muller_transform http://en.wikipedia.org/wiki/Box–Muller_transform
- davmre 12y agoYeah, the Gaussian trick is like programming in a high-level language: using existing abstractions lets you write clean and concise code, at the price of some efficiency versus the optimal engineered-from-scratch solution.
- carlob 12y agoI'm not sure I agree, I think the really high-level option is to use a library and not roll your own. For example: https://www.gnu.org/software/gsl/manual/html_node/Spherical-Vector-Distributions.html https://www.gnu.org/software/gsl/manual/html_node/Spherical-...
- davmre 12y agoFair enough. Though it looks like gsl_ran_dir_nd is implemented using normalized Gaussians, so in this case we're back where we started. :-)