Skip to content

Instantly share code, notes, and snippets.

@kjagiello
Created February 18, 2013 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjagiello/f3c069cffaa03b91800c to your computer and use it in GitHub Desktop.
Save kjagiello/f3c069cffaa03b91800c to your computer and use it in GitHub Desktop.
local
fun rmod x y = x - y * Real.realFloor (x / y);
val a = 16807.0;
val m = 2147483647.0;
val random_seed = ref 1.0;
in
fun random () =
let
val r = rmod (a * ! random_seed) m
in
random_seed := r;
Real.floor r
end
end;
fun removeElement ([], _) = []
| removeElement (first::rest, x) =
if first = x then
rest
else
first::removeElement (rest, x)
fun shuffle [] = []
| shuffle l =
let
val index = random () mod (length l)
val element = List.nth (l, index)
val l = removeElement (l, element)
in
element::shuffle l
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment