[m-rev.] diff: updates to random modules

Julien Fischer jfischer at opturion.com
Fri Sep 6 19:31:59 AEST 2019


Hi Mark,

On Fri, 6 Sep 2019, Mark Brown wrote:

> This addresses the discussion on the users list.

That looks fine, with the exception of the comment below.

> diff --git a/library/random.m b/library/random.m
> index 3b9873f..98322ca 100644
> --- a/library/random.m
> +++ b/library/random.m

...

> @@ -511,6 +535,30 @@ normal_floats(U, V, !R) :-
>          normal_floats(U, V, !R)
>      ).
> 
> +shuffle_list(L0, L, !R) :-
> +    A0 = array(L0),
> +    shuffle_array(A0, A, !R),
> +    L = array.to_list(A).
> +
> +shuffle_array(A0, A, !R) :-
> +    Lo = array.min(A0),
> +    Hi = array.max(A0),
> +    Sz = array.size(A0),
> +    shuffle_2(Lo, Lo, Hi, Sz, A0, A, !R).
> +
> +:- pred shuffle_2(int::in, int::in, int::in, int::in,
> +    array(T)::array_di, array(T)::array_uo, R::in, R::out) is det
> +    <= random(R).
> +
> +shuffle_2(I, Lo, Hi, Sz, !A, !R) :-
> +    ( if I > Hi then
> +        true
> +    else
> +        uniform_int_in_range(Lo, Sz, J, !R),
> +        swap_elems(I, J, !A),

The array module now exports array.swap/4 and array.unsafe_swap/4, the
latter of which you should be using here and in the unique version.
swap_elems/4 can be deleted.

Julien.



More information about the reviews mailing list