Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Commit

Permalink
Replace deprecated random module
Browse files Browse the repository at this point in the history
Replaced with crypto:rand_uniform functions.
  • Loading branch information
nickva committed Oct 2, 2017
1 parent 7c6a9cd commit c777cee
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
37 changes: 23 additions & 14 deletions test/gen_term.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ gen_atom(MaxSize) ->


gen_integer(_) ->
Value = case random:uniform() < 0.5 of
true -> random:uniform(127);
false -> random:uniform(16#FFFFFFFF)
Value = case random_uniform() < 0.5 of
true -> random_uniform(127);
false -> random_uniform(16#FFFFFFFF)
end,
case random:uniform() < 0.5 of
case random_uniform() < 0.5 of
true -> -1 * Value;
false -> Value
end.


gen_float(_) ->
random:uniform() * float(16#FFFFFFFF).
random_uniform() * float(16#FFFFFFFF).


gen_reference(_) ->
Expand All @@ -62,31 +62,31 @@ gen_reference(_) ->

gen_port(_) ->
Ports = erlang:ports(),
lists:nth(random:uniform(length(Ports)), Ports).
lists:nth(random_uniform(length(Ports)), Ports).


gen_pid(_) ->
Pids = erlang:processes(),
lists:nth(random:uniform(length(Pids)), Pids).
lists:nth(random_uniform(length(Pids)), Pids).


gen_tuple(MaxSize) ->
list_to_tuple(gen_list(MaxSize)).


gen_list(MaxSize) ->
Width = random:uniform(MaxSize),
Width = random_uniform(MaxSize),
[any(MaxSize-Width) || _ <- lists:seq(1, Width)].


gen_short_string(_) ->
Size = random:uniform(255),
[random:uniform(127) || _ <- lists:seq(1, Size)].
Size = random_uniform(255),
[random_uniform(127) || _ <- lists:seq(1, Size)].


gen_string(_) ->
Size = random:uniform(4096),
[random:uniform(127) || _ <- lists:seq(1, Size)].
Size = random_uniform(4096),
[random_uniform(127) || _ <- lists:seq(1, Size)].


gen_binary(MaxSize) ->
Expand All @@ -99,15 +99,15 @@ gen_bitstring(MaxSize) ->


gen_bignum(_) ->
16#FFFFFFFFFFFFFFFF + random:uniform(16#FFFFFFFF).
16#FFFFFFFFFFFFFFFF + random_uniform(16#FFFFFFFF).


gen_function(_) ->
choice(all_types()).


choice(Options) ->
lists:nth(random:uniform(length(Options)), Options).
lists:nth(random_uniform(length(Options)), Options).


value_types() ->
Expand All @@ -129,3 +129,12 @@ value_types() ->

all_types() ->
value_types() ++ [gen_tuple, gen_list].


random_uniform() ->
Range = 1 bsl 32,
crypto:rand_uniform(0, Range) / Range.


random_uniform(N) ->
crypto:rand_uniform(1, N + 1).
14 changes: 11 additions & 3 deletions test/khash_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ randomized_test_() ->
{setup,
local,
fun() ->
random:seed(erlang:now()),
Dict = dict:new(),
{ok, KHash} = khash:new(),
Actions = [
Expand Down Expand Up @@ -367,7 +366,7 @@ run_to_list({D, H}) ->
weighted_choice(Items0) ->
Items = lists:sort(Items0),
Sum = lists:sum([W || {W, _} <- Items]),
Choice = random:uniform() * Sum,
Choice = random_uniform() * Sum,
weighted_choice(Items, 0.0, Choice).

weighted_choice([], _, _) ->
Expand All @@ -379,7 +378,16 @@ weighted_choice([{_, I} | _], _, _) ->

random_key(D) ->
Keys = lists:usort(dict:fetch_keys(D) ++ [foo]),
lists:nth(random:uniform(length(Keys)), Keys).
lists:nth(random_uniform(length(Keys)), Keys).

random_val() ->
gen_term:any().


random_uniform() ->
Range = 1 bsl 32,
crypto:rand_uniform(0, Range) / Range.


random_uniform(N) ->
crypto:rand_uniform(1, N + 1).

0 comments on commit c777cee

Please sign in to comment.