Skip to content

Commit

Permalink
Update quickrand.
Browse files Browse the repository at this point in the history
  • Loading branch information
okeuday committed Sep 5, 2017
1 parent f1e73be commit f673323
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
4 changes: 4 additions & 0 deletions src/ChangeLog
@@ -1,6 +1,10 @@
# -*- coding: utf-8; tab-width: 4; -*-
# ex: set fenc=utf-8 sts=4 ts=4 et nomod:

2017-09-04 Michael Truog <mjtruog [at] gmail (dot) com>

* Update quickrand

2017-09-02 Michael Truog <mjtruog [at] gmail (dot) com>

* Fix the service configuration option response_timeout_immediate_max
Expand Down
4 changes: 2 additions & 2 deletions src/lib/README.markdown
Expand Up @@ -64,8 +64,8 @@ as external dependencies within CloudI (i.e., with a `cloudi_x_` prefix).
`Quick Erlang Random Number Generation`

- `https://github.com/okeuday/quickrand`
- `commit de0f2c7657723a223c5f8fc92a388e101697eae3 (v1.7.1)`
- `Wed Jun 7 15:51:52 PDT 2017`
- `commit 6072555ae3269a6a807002ad59b78efbb0104b28`
- `Mon Sep 4 19:29:44 PDT 2017`
- `MIT`

`reltool_util`
Expand Down
15 changes: 11 additions & 4 deletions src/lib/quickrand/src/quickrand.erl
Expand Up @@ -306,9 +306,14 @@ strong_uniform_range(Min, Max)
strong_float() ->
% 53 bits maximum for double precision floating point representation
% (need to use a maximum value of math:pow(2, 53) with extra bit,
% i.e. 16#1fffffffffffff + 1)
<<I:53/unsigned-integer, Bit:1, _:2>> = crypto:strong_rand_bytes(7),
(I + Bit) * ?DBL_EPSILON_DIV2.
% i.e. 1 + 16#1fffffffffffff)
<<Bit:1, I:53/unsigned-integer, _:2>> = crypto:strong_rand_bytes(7),
if
Bit == 1, I == 0 ->
1.0;
true ->
I * ?DBL_EPSILON_DIV2
end.

%%-------------------------------------------------------------------------
%% @doc
Expand Down Expand Up @@ -338,7 +343,9 @@ strong_floatM() ->
<<I:53/unsigned-integer, _:3>> = crypto:strong_rand_bytes(7),
if
I == 0 ->
?DBL_EPSILON_DIV2;
% almost never executes this case, an additional function call
% is necessary to have a uniform distribution
strong_floatM();
true ->
I * ?DBL_EPSILON_DIV2
end.
Expand Down
30 changes: 20 additions & 10 deletions src/lib/quickrand/src/quickrand_cache.erl
Expand Up @@ -81,8 +81,13 @@
float().

float() ->
<<I:53/unsigned-integer, Bit:1, _:2>> = rand_bytes(7),
(I + Bit) * ?DBL_EPSILON_DIV2.
<<Bit:1, I:53/unsigned-integer, _:2>> = rand_bytes(7),
if
Bit == 1, I == 0 ->
1.0;
true ->
I * ?DBL_EPSILON_DIV2
end.

%%-------------------------------------------------------------------------
%% @doc
Expand All @@ -94,8 +99,14 @@ float() ->
{float(), state()}.

float(State) ->
{<<I:53/unsigned-integer, Bit:1, _:2>>, NewState} = rand_bytes(7, State),
{(I + Bit) * ?DBL_EPSILON_DIV2, NewState}.
{<<Bit:1, I:53/unsigned-integer, _:2>>, NewState} = rand_bytes(7, State),
Value = if
Bit == 1, I == 0 ->
1.0;
true ->
I * ?DBL_EPSILON_DIV2
end,
{Value, NewState}.

%%-------------------------------------------------------------------------
%% @doc
Expand Down Expand Up @@ -136,7 +147,7 @@ floatM() ->
<<I:53/unsigned-integer, _:3>> = rand_bytes(7),
if
I == 0 ->
?DBL_EPSILON_DIV2;
floatM();
true ->
I * ?DBL_EPSILON_DIV2
end.
Expand All @@ -152,13 +163,12 @@ floatM() ->

floatM(State) ->
{<<I:53/unsigned-integer, _:3>>, NewState} = rand_bytes(7, State),
Value = if
if
I == 0 ->
?DBL_EPSILON_DIV2;
floatM(NewState);
true ->
I * ?DBL_EPSILON_DIV2
end,
{Value, NewState}.
{I * ?DBL_EPSILON_DIV2, NewState}
end.

%%-------------------------------------------------------------------------
%% @doc
Expand Down
2 changes: 1 addition & 1 deletion src/lib/quickrand/src/quickrand_internal.hrl
Expand Up @@ -29,7 +29,7 @@

-define(APPLICATION, quickrand).

% 1 / (16#1fffffffffffff + 1) =:= math:pow(2, -53) to provide [0.0 .. 1.0]
% 1 / (1 + 16#1fffffffffffff) =:= math:pow(2, -53) to provide [0.0 .. 1.0]
-define(DBL_EPSILON_DIV2, 1.1102230246251565e-16).

-define(BYTES_RESOLUTION, 4). % bytes
Expand Down

0 comments on commit f673323

Please sign in to comment.