Skip to content

Commit

Permalink
rebaring: use ?RNG_UNIFORM
Browse files Browse the repository at this point in the history
  • Loading branch information
fenollp committed May 19, 2018
1 parent 4846d48 commit 1118786
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
12 changes: 6 additions & 6 deletions src/proper_arith.erl
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ bounded_rand_non_neg_int(Const, Lim) when is_integer(Lim), Lim >= 0 ->

-spec rand_int(integer(), integer()) -> integer().
rand_int(Low, High) when is_integer(Low), is_integer(High), Low =< High ->
Low + ?RANDOM_MOD:uniform(High - Low + 1) - 1.
Low + ?RNG_UNIFORM(High - Low + 1) - 1.

%% When the range is large, skew the distribution to be more like that of an
%% unbounded random integer.
Expand All @@ -279,29 +279,29 @@ wide_range_rand_int(Const, Low, High) when Low >= 0 ->
wide_range_rand_int(Const, Low, High) when High =< 0 ->
High - bounded_rand_non_neg_int(Const, High - Low);
wide_range_rand_int(Const, Low, High) ->
case ?RANDOM_MOD:uniform(2) of
case ?RNG_UNIFORM(2) of
1 -> smart_rand_int(Const, 0, High);
2 -> smart_rand_int(Const, Low, 0)
end.

-spec rand_float(non_neg_integer()) -> float().
rand_float(Const) ->
X = rand_non_neg_float(Const),
case ?RANDOM_MOD:uniform(2) of
case ?RNG_UNIFORM(2) of
1 -> X;
2 -> -X
end.

-spec rand_non_neg_float(non_neg_integer()) -> float().
rand_non_neg_float(Const) when is_integer(Const), Const >= 0 ->
case ?RANDOM_MOD:uniform() of
case ?RNG_UNIFORM() of
1.0 -> rand_non_neg_float(Const);
X -> Const * zero_one_to_zero_inf(X)
end.

-spec rand_float(float(), float()) -> float().
rand_float(Low, High) when is_float(Low), is_float(High), Low =< High ->
Low + ?RANDOM_MOD:uniform() * (High - Low).
Low + ?RNG_UNIFORM() * (High - Low).

-spec zero_one_to_zero_inf(float()) -> float().
%% This function must return only non-negative values and map 0.0 to 0.0, but
Expand Down Expand Up @@ -329,7 +329,7 @@ distribute_tr(CreditsLeft, PeopleLeft, AccList) ->
-spec jumble([T]) -> [T].
%% @doc Produces a random permutation of a list.
jumble(List) ->
[X || {_, X} <- lists:sort([{?RANDOM_MOD:uniform(), X} || X <- List])].
[X || {_, X} <- lists:sort([{?RNG_UNIFORM(), X} || X <- List])].

-spec rand_choose([T,...]) -> {position(),T}.
rand_choose(Choices) when Choices =/= [] ->
Expand Down
20 changes: 10 additions & 10 deletions src/proper_gen_next.erl
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,14 @@ is_list_type(Type) ->
has_same_generator(Type, proper_types:list(proper_types:atom())).

list_choice(empty, Temp) ->
C = ?RANDOM_MOD:uniform(),
C = ?RNG_UNIFORM(),
C_Add = 0.5 * Temp,
if
C < C_Add -> add;
true -> nothing
end;
list_choice({list, GrowthCoefficient}, Temp) ->
C = ?RANDOM_MOD:uniform(),
C = ?RNG_UNIFORM(),
AddCoefficient = 0.3 * GrowthCoefficient,
DelCoefficient = 0.3 * (1- GrowthCoefficient),
C_Add = AddCoefficient * Temp,
Expand All @@ -343,7 +343,7 @@ list_choice({list, GrowthCoefficient}, Temp) ->
true -> nothing
end;
list_choice(vector, Temp) ->
C = ?RANDOM_MOD:uniform(),
C = ?RNG_UNIFORM(),
C_Mod = 0.5 * Temp,
if
C < C_Mod -> modify;
Expand All @@ -356,7 +356,7 @@ list_gen_sa(Type) ->
{ok, InternalType} = proper_types:find_prop(internal_type, Type),
ElementType = replace_generators(InternalType),
fun (Base, Temp) ->
GrowthCoefficient = (?RANDOM_MOD:uniform() * 0.8) + 0.1,
GrowthCoefficient = (?RNG_UNIFORM() * 0.8) + 0.1,
list_gen_internal(Base, Temp, InternalType, ElementType, GrowthCoefficient)
end.

Expand Down Expand Up @@ -570,12 +570,12 @@ union_gen_sa(Type) ->
end, [], Env) of
[] ->
%% generate new
Index = trunc(?RANDOM_MOD:uniform() * length(Env)) + 1,
Index = trunc(?RNG_UNIFORM() * length(Env)) + 1,
ET = lists:nth(Index, Env),
{ok, Value} = proper_gen:safe_generate(ET),
Value;
PossibleGens ->
C = ?RANDOM_MOD:uniform(),
C = ?RNG_UNIFORM(),
C_Kep = 0.3 * ?TEMP(Temp),
C_Chg = C_Kep + 0.3 * ?TEMP(Temp),
if
Expand All @@ -584,13 +584,13 @@ union_gen_sa(Type) ->
Base;
C < C_Chg ->
%% change choice
Index = trunc(?RANDOM_MOD:uniform() * length(Env)) + 1,
Index = trunc(?RNG_UNIFORM() * length(Env)) + 1,
ET = lists:nth(Index, Env),
{ok, Value} = proper_gen:safe_generate(ET),
Value;
true ->
%% modify amongst the possible
Index = trunc(?RANDOM_MOD:uniform() * length(PossibleGens)) + 1,
Index = trunc(?RNG_UNIFORM() * length(PossibleGens)) + 1,
ElementGen = lists:nth(Index, PossibleGens),
SAGen = replace_generators(ElementGen),
SAGen(Base, Temp)
Expand Down Expand Up @@ -768,11 +768,11 @@ get_size(Type, Temp) ->
not_found ->
%% use random initial size
%% proper:get_size(Type);
trunc(?RANDOM_MOD:uniform() * 21 + 1);
trunc(?RNG_UNIFORM() * 21 + 1);
{ok, Base} ->
%% alternate base size (max size is not accessible from the generator)
OffsetLimit = trunc(21 * Temp + 1),
Offset = trunc(?RANDOM_MOD:uniform() * OffsetLimit + 1),
Offset = trunc(?RNG_UNIFORM() * OffsetLimit + 1),
make_inrange(Base, Offset, 1, 42)
end,
set_cache_size(Type, Size),
Expand Down
6 changes: 2 additions & 4 deletions src/proper_sa.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@
-define(REHEAT_THRESHOLD, 5).
-define(RESTART_THRESHOLD, 100).

-define(RANDOM_PROBABILITY, (?RANDOM_MOD:uniform())).

-define(SA_DATA, proper_sa_data).
-define(SA_REHEAT_COUNTER, proper_sa_reheat_counter).

Expand Down Expand Up @@ -126,7 +124,7 @@ acceptance_function_standard(EnergyCurrent, EnergyNew, Temperature) ->
error:badarith -> 0.0
end,
%% if random probability is less, accept
?RANDOM_PROBABILITY < AcceptanceProbability
?RNG_UNIFORM() < AcceptanceProbability
end.

acceptance_function_normalized(EnergyCurrent, EnergyNew, Temperature) ->
Expand All @@ -143,7 +141,7 @@ acceptance_function_normalized(EnergyCurrent, EnergyNew, Temperature) ->
error:badarith -> 0.0
end,
%% if random probability is less, accept
?RANDOM_PROBABILITY < AcceptanceProbability
?RNG_UNIFORM() < AcceptanceProbability
end.

acceptance_function_hillclimbing(EnergyCurrent, EnergyNew, _Temperature) ->
Expand Down

0 comments on commit 1118786

Please sign in to comment.