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

Replace deprecated random module #7

Merged
merged 1 commit into from
Oct 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
language: erlang

otp_release:
- 19.3
- 18.3
- 17.5
- 17.4
- 17.1
- 17.0
- R16B03-1
- R14B04
- R14B02

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).