[erlang-questions] On OTP rand module difference between OTP 19 and OTP 20
Raimo Niskanen
<>
Fri Sep 1 14:13:46 CEST 2017
On Fri, Sep 01, 2017 at 04:00:59AM -0700, Michael Truog wrote:
> On 09/01/2017 01:54 AM, Raimo Niskanen wrote:
> > On Thu, Aug 31, 2017 at 10:29:34PM -0700, Michael Truog wrote:
> > :
> >> I have some examples that can make this desire a bit clearer:
> >>
> >> https://github.com/CloudI/cloudi_core/blob/a1c10a02245f0f4284d701a2ee5f07aad17f6e51/src/cloudi_core_i_runtime_testing.erl#L139-L149
> >>
> >> % use Box-Muller transformation to generate Gaussian noise
> >> % (G. E. P. Box and Mervin E. Muller,
> >> % A Note on the Generation of Random Normal Deviates,
> >> % The Annals of Mathematical Statistics (1958),
> >> % Vol. 29, No. 2 pp. 610–611)
> >> X1 = random(),
> >> X2 = PI2 * random(),
> >> K = StdDev * math:sqrt(-2.0 * math:log(X1)),
> >> Result1 = erlang:max(erlang:round(Mean + K * math:cos(X2)), 1),
> >> Result2 = erlang:max(erlang:round(Mean + K * math:sin(X2)), 1),
> >> sleep(Result2),
> > Why not use rand:normal/3?
> >
> > It uses the Ziggurat Method and is supposed to be much faster and
> > numerically more stable than the basic Box-Muller method.
> >
> The Box-Muller is simpler and producing 2 results instead of 1 . I believe I looked at the source code for rand:normal/3 and expected the Box-Muller to be faster only because it creates 2 results, though I should check that. I will have to investigate it more.
Simpler - yes.
The basic benchmark in rand_SUITE indicates that rand:normal() is only
about 50% slower than rand:uniform(1 bsl 58) (internal word size),
which I think is a very good number.
The Box-Muller transform method needs 4 calls to the 'math' module for
non-trivial floating point functions i.e log(), sqrt(), cos() and sin(),
which is why I think that "must" be slower.
But I have also not measured... :-/
Looking forward to hear your results!
--
/ Raimo Niskanen, Erlang/OTP, Ericsson AB
More information about the erlang-questions
mailing list