Skip to content

Instantly share code, notes, and snippets.

@gofer
Created August 15, 2018 16:36
Show Gist options
  • Save gofer/ea1d33eb497c918cfae91385eaa07817 to your computer and use it in GitHub Desktop.
Save gofer/ea1d33eb497c918cfae91385eaa07817 to your computer and use it in GitHub Desktop.
List Shuffle in StandardML/NJ
fun shuffle xs = let
val rand = let
val seed = let
val max = IntInf.fromInt (
case(Int.maxInt)
of (Option.SOME max) => max
| Option.NONE => raise Fail("Can't get Int.maxInt")
);
val time = Time.now ();
val (_, seed1) = IntInf.divMod (Time.toSeconds time, max);
val (_, seed2) = IntInf.divMod (Time.toMilliseconds time, max);
in (IntInf.toInt seed1, IntInf.toInt seed2) end;
in Random.rand seed end;
val ys = List.tabulate (List.length xs, fn n => Random.randInt rand);
fun pred ((p, q), (r, s)) = if q = s then EQUAL else (if q < s then LESS else GREATER);
val zs = qsort pred (ListPair.zip (xs, ys));
in List.map (fn (p, q) => p) zs end;
val xs = shuffle (List.tabulate (16, fn n => n + 1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment