aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/src/rand.erl
diff options
context:
space:
mode:
authorRaimo Niskanen <[email protected]>2017-04-21 15:08:10 +0200
committerRaimo Niskanen <[email protected]>2017-04-21 15:08:10 +0200
commitf214646911cf0cdb3d19d519908d2b06c4a4fff4 (patch)
treeaa90bee48213a5f8952309de8ed59a04a9732f50 /lib/stdlib/src/rand.erl
parent621cedccc78581330b9628c559b0d851c303564f (diff)
parentdff85f3d6fdb4b3453d863bf9208073564a9fcf2 (diff)
downloadotp-f214646911cf0cdb3d19d519908d2b06c4a4fff4.tar.gz
otp-f214646911cf0cdb3d19d519908d2b06c4a4fff4.tar.bz2
otp-f214646911cf0cdb3d19d519908d2b06c4a4fff4.zip
Merge branch 'rand/arbitrary_normal_distributions'
* rand/arbitrary_normal_distributions: rand: Support arbitrary normal distributions Conflicts: lib/stdlib/test/rand_SUITE.erl
Diffstat (limited to 'lib/stdlib/src/rand.erl')
-rw-r--r--lib/stdlib/src/rand.erl16
1 files changed, 15 insertions, 1 deletions
diff --git a/lib/stdlib/src/rand.erl b/lib/stdlib/src/rand.erl
index 2ee0ddb036..ab9731180f 100644
--- a/lib/stdlib/src/rand.erl
+++ b/lib/stdlib/src/rand.erl
@@ -31,7 +31,7 @@
export_seed/0, export_seed_s/1,
uniform/0, uniform/1, uniform_s/1, uniform_s/2,
jump/0, jump/1,
- normal/0, normal_s/1
+ normal/0, normal/2, normal_s/1, normal_s/3
]).
-compile({inline, [exs64_next/1, exsplus_next/1,
@@ -358,6 +358,13 @@ normal() ->
_ = seed_put(Seed),
X.
+%% normal/2: returns a random float with N(μ, σ²) normal distribution
+%% updating the state in the process dictionary.
+
+-spec normal(Mean :: number(), Variance :: number()) -> float().
+normal(Mean, Variance) ->
+ Mean + (math:sqrt(Variance) * normal()).
+
%% normal_s/1: returns a random float with standard normal distribution
%% The Ziggurat Method for generating random variables - Marsaglia and Tsang
%% Paper and reference code: http://www.jstatsoft.org/v05/i08/
@@ -378,6 +385,13 @@ normal_s(State0) ->
false -> normal_s(Idx, Sign, -X, State)
end.
+%% normal_s/3: returns a random float with normal N(μ, σ²) distribution
+
+-spec normal_s(Mean :: number(), Variance :: number(), state()) -> {float(), NewS :: state()}.
+normal_s(Mean, Variance, State0) when Variance > 0 ->
+ {X, State} = normal_s(State0),
+ {Mean + (math:sqrt(Variance) * X), State}.
+
%% =====================================================================
%% Internal functions