[m-rev.] diff: replace obsolete RNG in tabling tests

Julien Fischer jfischer at opturion.com
Wed Aug 11 20:50:58 AEST 2021


Replace obsolete RNG in tabling tests.

tests/tabling/expand*.m:
      Replace the use of the obsolete random number generator.

      Delete some unused module imports.

Julien.

diff --git a/tests/tabling/expand.m b/tests/tabling/expand.m
index 40cc5a8..ea846c5 100644
--- a/tests/tabling/expand.m
+++ b/tests/tabling/expand.m
@@ -20,14 +20,15 @@
  :- import_module list.
  :- import_module pair.
  :- import_module random.
-:- import_module require.
+:- import_module random.sfc32.
+:- import_module uint32.

  :- pragma require_feature_set([memo]).

  main(!IO) :-
-    random.init(0, RS0),
-    random.permutation(range(0, 1023), Perm, RS0, RS1),
-    choose_signs_and_enter(Perm, Solns, RS1, _RS),
+    sfc32.init(RNG, RS0),
+    random.shuffle_list(RNG, range(0, 1023), Perm, RS0, RS1),
+    choose_signs_and_enter(RNG, Perm, Solns, RS1, _RS),
      ( if test_tables(Solns, yes) then
          io.write_string("Test successful.\n", !IO)
      else
@@ -44,19 +45,19 @@ range(Min, Max) =
          [Min | range(Min + 1, Max)]
      ).

-:- pred choose_signs_and_enter(list(int)::in, assoc_list(int)::out,
-    random.supply::mdi, random.supply::muo) is det.
+:- pred choose_signs_and_enter(RNG::in, list(int)::in, assoc_list(int)::out,
+    State::di, State::uo) is det <= urandom(RNG, State).

-choose_signs_and_enter([], [], RS, RS).
-choose_signs_and_enter([N | Ns], [I - S | ISs], RS0, RS) :-
-    random.random(Random, RS0, RS1),
-    ( if Random mod 2 = 0 then
+choose_signs_and_enter(_, [], [], !RS).
+choose_signs_and_enter(RNG, [N | Ns], [I - S | ISs], !RS) :-
+    random.generate_uint32(RNG, Random, !RS),
+    ( if Random mod 2u32 = 0u32 then
          I = N
      else
          I = 0 - N
      ),
      sum(I, S),
-    choose_signs_and_enter(Ns, ISs, RS1, RS).
+    choose_signs_and_enter(RNG, Ns, ISs, !RS).

  :- pred test_tables(assoc_list(int)::in, bool::out) is det.

diff --git a/tests/tabling/expand_bitmap.m b/tests/tabling/expand_bitmap.m
index 33386cc..113ceda 100644
--- a/tests/tabling/expand_bitmap.m
+++ b/tests/tabling/expand_bitmap.m
@@ -20,8 +20,8 @@
  :- import_module int.
  :- import_module list.
  :- import_module random.
-:- import_module require.
-:- import_module std_util.
+:- import_module random.sfc32.
+:- import_module uint32.

  :- pragma require_feature_set([memo]).

@@ -29,18 +29,18 @@
      --->    record(T1, T1, T2).

  main(!IO) :-
-    random.init(0, RS0),
-    random.permutation(range(0, 1023), Perm, RS0, RS1),
+    sfc32.init(RNG, RS0),
+    random.shuffle_list(RNG, range(0, 1023), Perm, RS0, RS1),

      BM1 = bitmap.init(45, yes) ^ bits(20, 8) := 0b10001001,
      BM2 = bitmap.init(123, no) ^ bits(10, 8) := 0b10101010,
-    choose_signs_and_enter(Perm, BM1, Solns1, RS1, RS2),
+    choose_signs_and_enter(RNG, Perm, BM1, Solns1, RS1, RS2),
      ( if test_tables(Solns1, yes) then
          io.write_string("First test successful.\n", !IO)
      else
          io.write_string("First test unsuccessful.\n", !IO)
      ),
-    choose_signs_and_enter(Perm, BM2, Solns2, RS2, _RS),
+    choose_signs_and_enter(RNG, Perm, BM2, Solns2, RS2, _RS),
      ( if test_tables(Solns2, yes) then
          io.write_string("Second test successful.\n", !IO)
      else
@@ -57,19 +57,20 @@ range(Min, Max) =
          [Min | range(Min + 1, Max)]
      ).

-:- pred choose_signs_and_enter(list(int)::in, T::in, list(record(int, T))::out,
-    random.supply::mdi, random.supply::muo) is det.
+:- pred choose_signs_and_enter(RNG::in,
+    list(int)::in, T::in, list(record(int, T))::out,
+    State::di, State::uo) is det <= urandom(RNG, State).

-choose_signs_and_enter([], _, [], RS, RS).
-choose_signs_and_enter([N | Ns], A, [record(F, S, A) | ISs], RS0, RS) :-
-    random.random(Random, RS0, RS1),
-    ( if Random mod 2 = 0 then
+choose_signs_and_enter(_, [], _, [], !RS).
+choose_signs_and_enter(RNG, [N | Ns], A, [record(F, S, A) | ISs], !RS) :-
+    random.generate_uint32(RNG, Random, !RS),
+    ( if Random mod 2u32 = 0u32 then
          F = N
      else
          F = 0 - N
      ),
      sum(F, A, S),
-    choose_signs_and_enter(Ns, A, ISs, RS1, RS).
+    choose_signs_and_enter(RNG, Ns, A, ISs, !RS).

  :- pred test_tables(list(record(int, T))::in, bool::out) is det.

diff --git a/tests/tabling/expand_float.m b/tests/tabling/expand_float.m
index 98b1b44..1ab5329 100644
--- a/tests/tabling/expand_float.m
+++ b/tests/tabling/expand_float.m
@@ -21,14 +21,15 @@
  :- import_module list.
  :- import_module pair.
  :- import_module random.
-:- import_module require.
+:- import_module random.sfc32.
+:- import_module uint32.

  :- pragma require_feature_set([memo]).

  main(!IO) :-
-    random.init(0, RS0),
-    random.permutation(range(0, 1023), Perm, RS0, RS1),
-    choose_signs_and_enter(Perm, Solns, RS1, _RS),
+    sfc32.init(RNG, RS0),
+    random.shuffle_list(RNG, range(0, 1023), Perm, RS0, RS1),
+    choose_signs_and_enter(RNG, Perm, Solns, RS1, _RS),
      ( if test_tables(Solns, yes) then
          io.write_string("Test successful.\n", !IO)
      else
@@ -45,19 +46,19 @@ range(Min, Max) =
          [Min | range(Min + 1, Max)]
      ).

-:- pred choose_signs_and_enter(list(int)::in, assoc_list(float)::out,
-    random.supply::mdi, random.supply::muo) is det.
+:- pred choose_signs_and_enter(RNG::in, list(int)::in, assoc_list(float)::out,
+    State::di, State::uo) is det <= urandom(RNG, State).

-choose_signs_and_enter([], [], RS, RS).
-choose_signs_and_enter([N | Ns], [F - S | ISs], RS0, RS) :-
-    random.random(Random, RS0, RS1),
-    ( if Random mod 2 = 0 then
+choose_signs_and_enter(_, [], [], !RS).
+choose_signs_and_enter(RNG, [N | Ns], [F - S | ISs], !RS) :-
+    random.generate_uint32(RNG, Random, !RS),
+    ( if Random mod 2u32 = 0u32 then
          F = float(N)
      else
          F = float(0 - N)
      ),
      sum(F, S),
-    choose_signs_and_enter(Ns, ISs, RS1, RS).
+    choose_signs_and_enter(RNG, Ns, ISs, !RS).

  :- pred test_tables(assoc_list(float)::in, bool::out) is det.

diff --git a/tests/tabling/expand_poly.m b/tests/tabling/expand_poly.m
index def0aa6..25b1872 100644
--- a/tests/tabling/expand_poly.m
+++ b/tests/tabling/expand_poly.m
@@ -20,8 +20,8 @@
  :- import_module int.
  :- import_module list.
  :- import_module random.
-:- import_module require.
-:- import_module std_util.
+:- import_module random.sfc32.
+:- import_module uint32.

  :- pragma require_feature_set([memo]).

@@ -29,27 +29,27 @@
      --->    record(T1, T1, T2).

  main(!IO) :-
-    random.init(0, RS0),
-    random.permutation(range(0, 1023), Perm, RS0, RS1),
-    choose_signs_and_enter(Perm, 42, Solns1, RS1, RS2),
+    sfc32.init(RNG, RS0),
+    random.shuffle_list(RNG, range(0, 1023), Perm, RS0, RS1),
+    choose_signs_and_enter(RNG, Perm, 42, Solns1, RS1, RS2),
      ( if test_tables(Solns1, yes) then
          io.write_string("First test successful.\n", !IO)
      else
          io.write_string("First test unsuccessful.\n", !IO)
      ),
-    choose_signs_and_enter(Perm, [53], Solns2, RS2, RS3),
+    choose_signs_and_enter(RNG, Perm, [53], Solns2, RS2, RS3),
      ( if test_tables(Solns2, yes) then
          io.write_string("Second test successful.\n", !IO)
      else
          io.write_string("Second test unsuccessful.\n", !IO)
      ),
-    choose_signs_and_enter(Perm, [[64, 75]], Solns3, RS3, RS4),
+    choose_signs_and_enter(RNG, Perm, [[64, 75]], Solns3, RS3, RS4),
      ( if test_tables(Solns3, yes) then
          io.write_string("Third test successful.\n", !IO)
      else
          io.write_string("Third test unsuccessful.\n", !IO)
      ),
-    choose_signs_and_enter(Perm, record("a", "b", [1]), Solns4, RS4, _),
+    choose_signs_and_enter(RNG, Perm, record("a", "b", [1]), Solns4, RS4, _),
      ( if test_tables(Solns4, yes) then
          io.write_string("Fourth test successful.\n", !IO)
      else
@@ -66,19 +66,20 @@ range(Min, Max) =
          [Min | range(Min + 1, Max)]
      ).

-:- pred choose_signs_and_enter(list(int)::in, T::in, list(record(int, T))::out,
-    random.supply::mdi, random.supply::muo) is det.
+:- pred choose_signs_and_enter(RNG::in, list(int)::in,
+    T::in, list(record(int, T))::out,
+    State::di, State::uo) is det <= urandom(RNG, State).

-choose_signs_and_enter([], _, [], RS, RS).
-choose_signs_and_enter([N | Ns], A, [record(F, S, A) | ISs], RS0, RS) :-
-    random.random(Random, RS0, RS1),
-    ( if Random mod 2 = 0 then
+choose_signs_and_enter(_, [], _, [], !RS).
+choose_signs_and_enter(RNG, [N | Ns], A, [record(F, S, A) | ISs], !RS) :-
+    random.generate_uint32(RNG, Random, !RS),
+    ( if Random mod 2u32 = 0u32 then
          F = N
      else
          F = 0 - N
      ),
      sum(F, A, S),
-    choose_signs_and_enter(Ns, A, ISs, RS1, RS).
+    choose_signs_and_enter(RNG, Ns, A, ISs, !RS).

  :- pred test_tables(list(record(int, T))::in, bool::out) is det.

diff --git a/tests/tabling/expand_tuple.m b/tests/tabling/expand_tuple.m
index 4174414..ec6893f 100644
--- a/tests/tabling/expand_tuple.m
+++ b/tests/tabling/expand_tuple.m
@@ -22,8 +22,8 @@
  :- import_module int.
  :- import_module list.
  :- import_module random.
-:- import_module require.
-:- import_module std_util.
+:- import_module random.sfc32.
+:- import_module uint32.

  :- pragma require_feature_set([memo]).

@@ -31,21 +31,21 @@
      --->    record(T1, T1, T2).

  main(!IO) :-
-    random.init(0, RS0),
-    random.permutation(range(0, 1023), Perm, RS0, RS1),
-    choose_signs_and_enter(Perm, {}, Solns1, RS1, RS2),
+    sfc32.init(RNG, RS0),
+    random.shuffle_list(RNG, range(0, 1023), Perm, RS0, RS1),
+    choose_signs_and_enter(RNG, Perm, {}, Solns1, RS1, RS2),
      ( if test_tables(Solns1, yes) then
          io.write_string("First test successful.\n", !IO)
      else
          io.write_string("First test unsuccessful.\n", !IO)
      ),
-    choose_signs_and_enter(Perm, {'a', [1, 2]}, Solns2, RS2, RS3),
+    choose_signs_and_enter(RNG, Perm, {'a', [1, 2]}, Solns2, RS2, RS3),
      ( if test_tables(Solns2, yes) then
          io.write_string("Second test successful.\n", !IO)
      else
          io.write_string("Second test unsuccessful.\n", !IO)
      ),
-    choose_signs_and_enter(Perm, {{'a', 'b'}, 3, 4}, Solns3, RS3, _),
+    choose_signs_and_enter(RNG, Perm, {{'a', 'b'}, 3, 4}, Solns3, RS3, _),
      ( if test_tables(Solns3, yes) then
          io.write_string("Third test successful.\n", !IO)
      else
@@ -62,19 +62,20 @@ range(Min, Max) =
          [Min | range(Min + 1, Max)]
      ).

-:- pred choose_signs_and_enter(list(int)::in, T::in, list(record(int, T))::out,
-    random.supply::mdi, random.supply::muo) is det.
+:- pred choose_signs_and_enter(RNG::in, list(int)::in, T::in,
+    list(record(int, T))::out,
+    State::di, State::uo) is det <= urandom(RNG, State).

-choose_signs_and_enter([], _, [], RS, RS).
-choose_signs_and_enter([N | Ns], A, [record(F, S, A) | ISs], RS0, RS) :-
-    random.random(Random, RS0, RS1),
-    ( if Random mod 2 = 0 then
+choose_signs_and_enter(_, [], _, [], !RS).
+choose_signs_and_enter(RNG, [N | Ns], A, [record(F, S, A) | ISs], !RS) :-
+    random.generate_uint32(RNG, Random, !RS),
+    ( if Random mod 2u32 = 0u32 then
          F = N
      else
          F = 0 - N
      ),
      sum(F, A, S),
-    choose_signs_and_enter(Ns, A, ISs, RS1, RS).
+    choose_signs_and_enter(RNG, Ns, A, ISs, !RS).

  :- pred test_tables(list(record(int, T))::in, bool::out) is det.

diff --git a/tests/tabling/expand_tuple2.m b/tests/tabling/expand_tuple2.m
index 49f99e7..d6f338c 100644
--- a/tests/tabling/expand_tuple2.m
+++ b/tests/tabling/expand_tuple2.m
@@ -19,9 +19,9 @@
  :- import_module int.
  :- import_module list.
  :- import_module random.
-:- import_module require.
-:- import_module std_util.
+:- import_module random.sfc32.
  :- import_module string.
+:- import_module uint32.

  :- pragma require_feature_set([memo]).

@@ -29,9 +29,9 @@
      --->  record(T1, T2, T1, T2).

  main(!IO) :-
-    random.init(0, RS0),
-    random.permutation(range(0, 1023), Perm, RS0, RS1),
-    choose_signs_and_enter(Perm, "0", Solns1, RS1, _RS2),
+    sfc32.init(RNG, RS0),
+    random.shuffle_list(RNG, range(0, 1023), Perm, RS0, RS1),
+    choose_signs_and_enter(RNG, Perm, "0", Solns1, RS1, _RS2),
      ( if test_tables(Solns1, yes) then
          io.write_string("Test successful.\n", !IO)
      else
@@ -48,20 +48,20 @@ range(Min, Max) =
          [Min | range(Min + 1, Max)]
      ).

-:- pred choose_signs_and_enter(list(int)::in, string::in,
+:- pred choose_signs_and_enter(RNG::in, list(int)::in, string::in,
      list(record(int, string))::out,
-    random.supply::mdi, random.supply::muo) is det.
+    State::di, State::uo) is det <= urandom(RNG, State).

-choose_signs_and_enter([], _, [], RS, RS).
-choose_signs_and_enter([N | Ns], A, [record(F, A, S, B) | ISs], RS0, RS) :-
-    random.random(Random, RS0, RS1),
-    ( if Random mod 2 = 0 then
+choose_signs_and_enter(_, [], _, [], !RS).
+choose_signs_and_enter(RNG, [N | Ns], A, [record(F, A, S, B) | ISs], !RS) :-
+    random.generate_uint32(RNG, Random, !RS),
+    ( if Random mod 2u32 = 0u32 then
          F = N
      else
          F = 0 - N
      ),
      sum({F, A}, {S, B}),
-    choose_signs_and_enter(Ns, A, ISs, RS1, RS).
+    choose_signs_and_enter(RNG, Ns, A, ISs, !RS).

  :- pred test_tables(list(record(int, string))::in, bool::out) is det.



More information about the reviews mailing list