Eliminating random module calls from the OTP code

It’s been a while, or at least 6 years :), since the old random module has been declared deprecated from OTP 19.

As of commit 66dd3c397b5dd68cb087a1a830f25c62c5d1d2ad in GitHub Erlang/OTP repository, I still observe the following function calls to the random module. It’s interesting to see that still many of OTP’s common test case codes are using the random module, and I guess most of them could be replaced by the corresponding rand module function calls.

I’d be glad if anyone in the OTP team could take a look at this issue.

(PS: I know some of the listings below include the documentation pieces, not running code.)

./lib/dialyzer/test/indent_SUITE_data/src/simple/is_rec.erl
30:    random:uniform(3).

./lib/dialyzer/test/indent_SUITE_data/src/map_galore.erl
1512:    random:seed(Seed),
1530:			Int = random:uniform(N*4) - N*2,
1561:    {K1,V1} = lists:nth(random:uniform(length(ML1)), ML1),
1652:				KI = random:uniform(size(Keys)),
1654:				KV = element(random:uniform(size(K)), K),
1694:		  random:uniform(size(Leafs)) + 3;
1696:		  random:uniform(3);
1698:		  random:uniform(size(Leafs)+3)
1702:	    Size = random:uniform(size(Leafs)),
1716:	    Size = random:uniform(size(Leafs)),
1725:		    case random:uniform(4) of

./lib/dialyzer/test/options2_SUITE_data/src/kernel/global.erl
1864:	    random:seed(A1, A2, A3 + erlang:phash(node(), 100000));
1871:    T = random:uniform(Tmax),

./lib/dialyzer/test/small_SUITE_data/src/false_false.erl
23:  case new_execute_cmd(random:uniform(2)) of

./lib/dialyzer/test/r9c_SUITE_data/src/asn1/asn1ct_value.erl
261:    random:seed(A1,A2,A3),
262:    random:uniform(Upper).

./lib/dialyzer/test/r9c_SUITE_data/src/mnesia/mnesia_lib.erl
842:	    random:seed(X, Y, Z),
843:	    Time = Dup + random:uniform(MaxIntv),
847:	    Time = Dup + random:uniform(MaxIntv),

./lib/dialyzer/test/map_SUITE_data/src/map_galore.erl
1512:    random:seed(Seed),
1530:			Int = random:uniform(N*4) - N*2,
1561:    {K1,V1} = lists:nth(random:uniform(length(ML1)), ML1),
1652:				KI = random:uniform(size(Keys)),
1654:				KV = element(random:uniform(size(K)), K),
1694:		  random:uniform(size(Leafs)) + 3;
1696:		  random:uniform(3);
1698:		  random:uniform(size(Leafs)+3)
1702:	    Size = random:uniform(size(Leafs)),
1716:	    Size = random:uniform(size(Leafs)),
1725:		    case random:uniform(4) of

./lib/dialyzer/test/opaque_SUITE_data/src/simple/is_rec.erl
30:    random:uniform(3).

./lib/stdlib/test/random_SUITE.erl
62:    _ = spawn(fun() -> Self ! random:uniform() end),
66:    _ = spawn(fun() -> random:seed(),
67:		       Self ! random:uniform() end),
79:		      random:seed(S1,S2,S3),
81:					      (_, Out) -> [random:uniform(10000)|Out]
87:		      random:seed(Seed),
89:					      (_, Out) -> [random:uniform(10000)|Out]
107:    X = random:uniform(Top),

./lib/stdlib/test/rand_SUITE.erl
1220:                {random, random:seed(os:timestamp()), get(random_seed)};

./lib/stdlib/test/io_proto_SUITE.erl
463:    random:seed(1240, 900586, 553728),
641:    X = case random:uniform(20) of
643:	    A0 when A0 =< 3 -> random:uniform(16#10FFFF);
644:	    A1 when A1 =< 6 -> random:uniform(16#10FFFF - 16#7F) + 16#7F;
645:	    A2 when A2 =< 12 -> random:uniform(16#10FFFF - 16#7FF) + 16#7FF;
646:	    _ -> random:uniform(16#10FFFF - 16#FFFF) + 16#FFFF

./lib/stdlib/test/erl_lint_SUITE.erl
2244:                  {erlang:now(), random:seed0(), random:seed(1, 2, 3),
2245:                   random:uniform(), random:uniform(42)}.

./lib/stdlib/doc/src/random.xml
103:random:seed(erlang:phash2([node()]),

./lib/stdlib/doc/src/notes.xml
5844:	    case was random:seed(0,0,0) that produced a series of
7523:	    <c>random:seed/1</c>, which takes a three-tuple.</p>
7807:	    <p>The argument passed to <c>random:uniform/1</c> must

./lib/inets/test/erl_make_certs.erl
182:    {#'OTPTBSCertificate'{serialNumber = trunc(random:uniform()*100000000)*10000 + 1,

./lib/mnesia/examples/bench/bench_populate.erl
107:    random:seed(),
115:    Name = list_to_binary([random:uniform(26) + $A - 1]),
116:    GroupId = random:uniform(C#config.n_groups) - 1,
137:    random:seed(),
161:    case random:uniform(100) < (90 + 1) of
176:    random:seed(),
5 Likes

I have created the following pull request:

to eliminate the use of random in io_proto_SUITE and use of random in the example in the User’s Guide for cprof.

The use of random in erl_lint_SUITE is deliberate, because it tests the handling of deprecated functions. (The code is only analyzed by erl_lint, never actually run.)

The code in the Dialyzer test suites that references random is only analyzed by Dialyzer, never actually run. Some of that code is copies of actual OTP applications from the R9C release. We will not update those test suites.

4 Likes

I will write an internal ticket to put this on our ToDo list…

Too late - Björn beat me to it

Cheers!

4 Likes

There are still the uses of random in the Mnesia and inets test suites.

3 Likes

The inets test suites should be easy to fix by modernizing how certificate test data is generated which I think we should do anyhow.

4 Likes

Björn, Raimo, Ingela: very much appreciated your quick response!

3 Likes

modernization proposal for inets (changes for reviving the test suite also included)

3 Likes