fp2-w1: RandomGetallen
[fp1415.git] / fp2 / week1 / camil / RandomGetallen.icl
1 implementation module RandomGetallen
2
3 import StdEnv, Random
4
5 random_n :: Int RandomSeed -> ([Int],RandomSeed)
6 random_n 0 seed = ([], seed)
7 random_n n seed = ([r:rs], s`)
8 where
9 (rs,s`) = random_n (n-1) s
10 (r,s) = random seed
11
12 random_inf :: RandomSeed -> [Int]
13 random_inf s = iterateSt random s
14
15 iterateSt :: (s -> (a,s)) s -> [a]
16 iterateSt f s = [a : iterateSt f s`]
17 where
18 (a,s`) = f s
19
20 shuffle :: [a] RandomSeed -> [a]
21 shuffle xs s = (perms xs)!!(fst (random s) rem (factorial (length xs)))
22
23 perms :: [a] -> [[a]]
24 perms [] = [[]]
25 perms xs = [[xs!!i : xs`] \\ i <- [0..length xs - 1] , xs` <- perms (take i xs ++ drop (i+1) xs)]
26
27 factorial :: Int -> Int
28 factorial 0 = 1
29 factorial n = n * factorial (n-1)