Created
February 18, 2013 09:22
-
-
Save kjagiello/f3c069cffaa03b91800c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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