mirror of
https://github.com/Mercury-Language/mercury.git
synced 2026-04-17 02:13:54 +00:00
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.
41 lines
992 B
Mathematica
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
|
|
).
|
|
|