Fix a bug in the implementation of the pseudo-random number generator

Cristian Greco <>
Wed Oct 6 04:19:11 CEST 2010


Hi,

the current implementation of the Wichmann-Hill prng contains an error,
which is fixed in the following branch.

  git fetch :cristiangreco/otp.git cg/fix-prng

More details provided in the following mail.


This commit fixes an error in the mathematical formula of the
Wichmann-Hill pseudo-random number generator. In particular, the
implementation used until now produces sequences which differ from the
expected ones by an extra starting number, which is instead the very
last value of the sequence. This bug amplified the effect of extremely
correlated initial numbers when seeding different generators with very
similar seed values.
---
 lib/stdlib/src/random.erl |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/stdlib/src/random.erl b/lib/stdlib/src/random.erl
index 01227c2..d94722c 100644
--- a/lib/stdlib/src/random.erl
+++ b/lib/stdlib/src/random.erl
@@ -86,7 +86,7 @@ uniform() ->
     B2 = (A2*172) rem 30307,
     B3 = (A3*170) rem 30323,
     put(random_seed, {B1,B2,B3}),
-    R = A1/30269 + A2/30307 + A3/30323,
+    R = B1/30269 + B2/30307 + B3/30323,
     R - trunc(R).
 
 %% uniform(N) -> I
@@ -110,7 +110,7 @@ uniform_s({A1, A2, A3}) ->
     B1 = (A1*171) rem 30269,
     B2 = (A2*172) rem 30307,
     B3 = (A3*170) rem 30323,
-    R = A1/30269 + A2/30307 + A3/30323,
+    R = B1/30269 + B2/30307 + B3/30323,
     {R - trunc(R), {B1,B2,B3}}.
 
 %% uniform_s(N, State) -> {I, NewState}
-- 
1.7.1


Thanks,
--
Cristian Greco
GPG key ID: 0xCF4D32E4
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-patches/attachments/20101006/300174a4/attachment.bin>


More information about the erlang-patches mailing list