diff options
| author | Mart Lubbers | 2015-04-21 14:25:25 +0200 | 
|---|---|---|
| committer | Mart Lubbers | 2015-04-21 14:25:25 +0200 | 
| commit | 892075a5f701177235c0296c28899563836b42e2 (patch) | |
| tree | 0790b9518f7aac9bfe732ce030b4f467f5045229 /fp2/week1/mart | |
| parent | Added s-numbers (diff) | |
rare zooi
Diffstat (limited to 'fp2/week1/mart')
| -rw-r--r-- | fp2/week1/mart/Random.dcl | 19 | ||||
| -rw-r--r-- | fp2/week1/mart/Random.icl | 20 | ||||
| -rwxr-xr-x | fp2/week1/mart/RandomGetallen | bin | 0 -> 105976 bytes | |||
| -rw-r--r-- | fp2/week1/mart/RandomGetallen.dcl | 7 | ||||
| -rw-r--r-- | fp2/week1/mart/RandomGetallen.icl | 33 | 
5 files changed, 79 insertions, 0 deletions
| diff --git a/fp2/week1/mart/Random.dcl b/fp2/week1/mart/Random.dcl new file mode 100644 index 0000000..47a7c18 --- /dev/null +++ b/fp2/week1/mart/Random.dcl @@ -0,0 +1,19 @@ +definition module Random + + // Random number generator voor Linux gebruikers + // interface compatible met Random.dcl (helaas) + // -- mschool@science.ru.nl + +import StdFile + +:: RandomSeed  + +// nullRandomSeed generates a fixed RandomSeed +nullRandomSeed :: RandomSeed + +// GetNewRandomSeed generates a good RandomSeed, using /dev/urandom +getNewRandomSeed :: !*env -> (!RandomSeed, !*env) | FileSystem env + +// Given a RandomSeed, Random generates a random number and a new RandomSeed. +random :: !RandomSeed -> .(!Int, !RandomSeed) + diff --git a/fp2/week1/mart/Random.icl b/fp2/week1/mart/Random.icl new file mode 100644 index 0000000..b6e0768 --- /dev/null +++ b/fp2/week1/mart/Random.icl @@ -0,0 +1,20 @@ +implementation module Random + +import StdFile, StdList, StdMisc, StdArray, Random + +:: RandomSeed :== Int + +nullRandomSeed :: RandomSeed +nullRandomSeed = 0 + +getNewRandomSeed :: !*env -> (!RandomSeed, !*env) | FileSystem env +getNewRandomSeed env +# (ok, src, env) = sfopen "/dev/urandom" FReadData env +| not ok => abort "could not open /dev/urandom" +# (bytes, src)   = sfreads src 4 +  seed           = foldl (\x y->(x<<8)+toInt y) 0 [c \\ c<-:bytes] +| otherwise => (seed, env) + +random :: !RandomSeed -> .(!Int, !RandomSeed) +random seed = (seed>>16 bitand 0xFFFF, seed*0x08088405+1) + diff --git a/fp2/week1/mart/RandomGetallen b/fp2/week1/mart/RandomGetallenBinary files differ new file mode 100755 index 0000000..0482437 --- /dev/null +++ b/fp2/week1/mart/RandomGetallen diff --git a/fp2/week1/mart/RandomGetallen.dcl b/fp2/week1/mart/RandomGetallen.dcl new file mode 100644 index 0000000..66a2c6c --- /dev/null +++ b/fp2/week1/mart/RandomGetallen.dcl @@ -0,0 +1,7 @@ +definition module RandomGetallen
 +
 +import Random
 +
 +random_n	:: Int RandomSeed -> ([Int],RandomSeed)
 +random_inf	::     RandomSeed ->  [Int]
 +//shuffle		:: [a] RandomSeed ->  [a]
 diff --git a/fp2/week1/mart/RandomGetallen.icl b/fp2/week1/mart/RandomGetallen.icl new file mode 100644 index 0000000..b756c91 --- /dev/null +++ b/fp2/week1/mart/RandomGetallen.icl @@ -0,0 +1,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)]
 | 
