Files
mercury/tests/hard_coded/random_shuffle.m
Mark Brown 3a83702562 Changes per the discussion on the users list.
library/random.m:
	Add predicates shuffle_list/{4,5} and shuffle_array/{4,5}.

library/random.m:
library/random.sfc*.m:
	Improve comments.

tests/hard_coded/Mmakefile:
tests/hard_coded/random_shuffle*.{m,exp}:
	Test the new predicates.
2019-09-06 15:51:02 +10:00

41 lines
992 B
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 sts=4 et ft=mercury
%---------------------------------------------------------------------------%
:- module random_shuffle.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int.
:- import_module list.
:- import_module random.
:- import_module random.sfc32.
main(!IO) :-
List = 1 `..` 100,
sfc32.init(P, S),
make_io_urandom(P, S, M, !IO),
test(M, List, 10, !IO).
:- pred test(M::in, list(int)::in, int::in, io::di, io::uo) is det
<= urandom(M, io).
test(M, List, Count, !IO) :-
( if Count > 0 then
shuffle_list(M, List, Shuffled, !IO),
sort_and_remove_dups(Shuffled, Sorted),
( if Sorted = List then
io.write_string("Passed.\n", !IO)
else
io.write_string("Failed!\n", !IO)
),
test(M, List, Count - 1, !IO)
else
true
).