<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">I'm not sure where I got this PICK function from, and don't understand why it's written as it is, so I wanted to test it for randomness. It seems random enough. But if I understand the algorithm correctly, instead of selecting one of the elements from the list, it eliminates all the elements but one and that's the value it returns. Seems like a roundabout way of doing it. Comments?<br><br>Also, is there a more direct way of printing an array?<br><br>Output below.<br><br>Michael<br><br>=================<br><br>import System.Random<br>import Data.Array.IO<br><br>pick :: [a] -> IO a<br>pick [] = undefined<br>pick [x] = do return x<br>pick (x:xs) = pick' x xs (2 :: Int)<br><br>pick' :: (Num p, Random p) => t -> [t] -> p -> IO t<br>pick' curr []
_ = do return curr<br>pick' curr (next:rest) prob<br> = do r <- getStdRandom (randomR (1,prob))<br> let curr' = if r == 1 then next else curr<br> pick' curr' rest (prob+1)<br><br>main = do arr <- newArray (1,9) 0 :: IO (IOArray Int Int)<br> doLoop arr [1,2,3,4,5,6,7,8,9] 0<br> <br>doLoop arr z k = do p <- pick z<br> a <- readArray arr p<br> writeArray arr p (a+1)<br> if k >
10000<br> then do<br> v <- readArray arr 1<br> print v<br> v <- readArray arr 2<br> print v<br> v <- readArray arr
3<br> print v<br> v <- readArray arr 4<br> print v<br> v <- readArray arr 5<br> print v<br> v <- readArray arr
6<br> print v<br> v <- readArray arr 7<br> print v<br> v <- readArray arr 8<br> print v<br> v <- readArray arr
9<br> print v<br> else do<br> doLoop arr z (k+1)<br><br>===============<br><br>[michael@localhost ~]$ runhaskell array1.hs<br>1110<br>1117<br>1080<br>1169<br>1112<br>1119<br>1137<br>1084<br>1074<br>[michael@localhost ~]$ <br><br></td></tr></table><br>