summaryrefslogtreecommitdiff
path: root/fp2/week1/camil/RandomGetallen.icl
blob: a0a21bf5f1cf59fbe18bfe1ef3f046cb8efcdcc0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
implementation module RandomGetallen

import StdEnv, Random

random_n :: Int RandomSeed -> ([Int],RandomSeed)
random_n 0 seed = ([], seed)
random_n n seed = ([r:rs], s`)
where 
	(rs,s`) = random_n (n-1) s
	(r,s) = random seed	

random_inf :: RandomSeed -> [Int]
random_inf s = iterateSt random s

iterateSt :: (s -> (a,s)) s -> [a]
iterateSt f s = [a : iterateSt f s`]
where
	(a,s`) = f s

shuffle :: [a] RandomSeed -> [a]
shuffle xs s = (perms xs)!!(fst (random s) rem (factorial (length xs)))

perms :: [a] -> [[a]]
perms [] = [[]]
perms xs = [[xs!!i : xs`] \\ i <- [0..length xs - 1] , xs` <- perms (take i xs ++ drop (i+1) xs)]

factorial :: Int -> Int
factorial 0 = 1
factorial n = n * factorial (n-1)