summaryrefslogtreecommitdiff
path: root/fp2/week1/mart/RandomGetallen.icl
blob: b756c91a95f434a986e264c819603aa68ed230fe (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
30
31
32
33
implementation module RandomGetallen

import StdEnv, Random

//Start :: *World -> ([Int],*World)
//Start world
//# (rs,world)	= getNewRandomSeed world
//= (shuffle [1..10] rs,world)


Start = shuffle [1..10] nullRandomSeed

random_n :: Int RandomSeed -> ([Int],RandomSeed)
random_n n seed = seqList (repeatn n random) seed

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

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

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

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

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