Difference between revisions of "Examples/Random list"

From HaskellWiki
Jump to navigation Jump to search
(looks like someone used two different names for the same thing)
(→‎Create a random list: Added link to "Random list")
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
[[Category:Code]]
 
[[Category:Code]]
  +
  +
== Create a random list ==
   
 
Generate a random list of numbers, without using the
 
Generate a random list of numbers, without using the
Line 16: Line 18:
 
randomlist n = take n . unfoldr (Just . random)
 
randomlist n = take n . unfoldr (Just . random)
 
</haskell>
 
</haskell>
  +
  +
See also the wiki page [[Random list]]
  +
  +
== Delete an element at random ==
  +
  +
unpick and unpick' are by osfameron and are from http://osfameron.vox.com/library/post/more-random-fun.html (no explicit license)
  +
removeOne is by Chris Kuklewicz (BSD3 licence, 2007)
  +
  +
<haskell>
  +
import System.Random
  +
import Control.Monad.State.Lazy
  +
import Debug.Trace -- for removeOne' demonstration
  +
</haskell>
  +
  +
The unpick function and its helper unpick' are strict in the entire list being operated on (forcing it all into memory at once). And IO [a] cannot lazily return any initial values.
  +
  +
<haskell>
  +
unpick :: [a] -> IO [a]
  +
unpick [] = undefined
  +
unpick [x] = return []
  +
unpick (x:xs) = do zs <- unpick' [] [x] xs 2
  +
return (reverse zs)
  +
  +
unpick' :: (Num p, Random p) => [t] -> [t] -> [t] -> p -> IO [t]
  +
unpick' curr _ [] _ = return curr
  +
unpick' curr orig (next:rest) prob
  +
= do r <- getStdRandom (randomR (1,prob))
  +
let curr' = if r == 1 then orig else (next:curr)
  +
unpick' curr' (next:orig) rest (prob+1)
  +
</haskell>
  +
  +
To run in the IO Monad just use <hask>(getStdRandom . removeOne) :: [a] -> IO [a].</hask>
  +
<hask>removeOne</hask> returns the output list lazily as soon as it has decided not to delete any element in a prefix of the input list.
  +
The resulting list is constructed efficiently, with no wasted intermediate list construction. <hask>removeOne</hask> allows any output it generates to be garbage collected, it holds no references to it.
  +
  +
<hask>removeOne</hask> is presented in curried form, without a binding for the
  +
RandomGen g. The StdGen is hidden inside a State monad. <hask>removeOne</hask> is designed for use with Strict.Lazy. It may not be optimal to use with <hask>Strict.Strict</hask>.
  +
  +
Like <hask>tail</hask> this function is partial and will produce an error if given the empty list.
  +
  +
<haskell>
  +
removeOne :: (RandomGen g) => [a] -> g -> ([a],g)
  +
removeOne [] = error "Cannot removeOne from empty list"
  +
removeOne whole@(_:xs) = runState (helper whole xs 0 1) where
  +
</haskell>
  +
  +
The laziness is needed in helper to make <hask>rest</hsak> a lazy thunk. The
  +
<hask>start</hask> list parameter to helper is a suffix of <hask>whole</hask> that has the current candidate for deletion as its head. <hask>oldIndex</hask> is the index of the current candidate for deletion in the <hask>whole</hask> list. <hask>here</hask> is a suffix of <hask>whole</hask> with the <hask>index</hask><sup>th</sup> element of whole as its head. <hask>randomR</hask> decides if the head of <hask>here</hask> replaces the head of <hask>start</hask> as the candidate to remove. If it does replace the old candidate then a prefix of <hask>start</hask> of length <hask>(index-oldIndex)</hask> is immediately output, counted off by prependSome.
  +
  +
Assert <hask>"start"</hask> is never <hask>[]</hask>.
  +
Assert <hask>0 <= oldIndex < index</hask>.
  +
  +
<haskell>
  +
helper start [] oldIndex index = return (tail start)
  +
helper start here@(_:ys) oldIndex index = do
  +
r <- State (randomR (0,index))
  +
if r==0 then do rest <- helper here ys index $! succ index
  +
return (prependSome (index-oldIndex) start rest)
  +
else helper start ys oldIndex $! succ index
  +
</haskell>
  +
  +
I assert that <hask>prependSome n xs ys == take n xs ++ ys</hask> but slightly
  +
optimized (without depending on the compiler). Assert <hask>n >= length xs</hask>.
  +
  +
<haskell>
  +
prependSome :: Int -> [a] -> [a] -> [a]
  +
prependSome 0 _ rest = rest
  +
prependSome n (x:xs) rest = x : prependSome (pred n) xs rest
  +
prependSome _ [] _ = error "impossible error in removeOne.prependSome"
  +
</haskell>
  +
  +
<hask>removeOne</hask> is a tracing version for demonstration below:
  +
  +
<haskell>
  +
removeOne' :: (Show a,RandomGen g) => [a] -> g -> ([a],g)
  +
removeOne' [] _ = error "Cannot removeOne from empty list"
  +
removeOne' whole@(x:xs) g = runState (helper whole xs 0 1) g where
  +
helper start [] oldIndex index = return (tail start)
  +
helper start here@(_:ys) oldIndex index = do
  +
r <- State (randomR (0,index))
  +
if r==0 then do rest <- helper here ys index $! succ index
  +
let rest' = trace "." rest
  +
return (prependSome (index-oldIndex) start rest')
  +
else do let ys' = trace "_" ys
  +
helper start ys' oldIndex $! succ index
  +
</haskell>
  +
  +
I will use <hask>removeOne</hask> to demonstrate when random decisions to drop
  +
elements are made. This also demonstrates that <hask>removeOne</hask> is lazy,
  +
returning elements as soon as the removal decision has moved on to a
  +
later element (the "." is output instead of "_").
  +
  +
The element after the last "." is the one actually removed,
  +
defaulting to the first element.
  +
  +
Since the probability of "." decreases, the average length of the run of output produced by <hask>appendSome</hask> increases as the list is processed.
  +
  +
<haskell>
  +
*Main> getStdRandom (removeOne' [1..10])
  +
[1.
  +
_
  +
_
  +
,2,3,4.
  +
_
  +
_
  +
_
  +
_
  +
,5,6,7,8,9.
  +
]
  +
*Main> getStdRandom (removeOne' [1..10])
  +
_
  +
_
  +
[1,2,3.
  +
_
  +
_
  +
_
  +
_
  +
_
  +
_
  +
,5,6,7,8,9,10]
  +
*Main> getStdRandom (removeOne' [1..10])
  +
_
  +
_
  +
_
  +
_
  +
_
  +
_
  +
_
  +
_
  +
_
  +
[2,3,4,5,6,7,8,9,10]
  +
*Main> getStdRandom (removeOne' [1..10])
  +
[1.
  +
_
  +
_
  +
_
  +
_
  +
_
  +
_
  +
_
  +
_
  +
,3,4,5,6,7,8,9,10]
  +
*Main> getStdRandom (removeOne' [1..10])
  +
[1.
  +
,2.
  +
_
  +
_
  +
_
  +
_
  +
_
  +
_
  +
_
  +
,4,5,6,7,8,9,10]
  +
*Main> getStdRandom (removeOne' [1..10])
  +
[1.
  +
_
  +
_
  +
_
  +
_
  +
,2,3,4,5,6.
  +
_
  +
_
  +
,7,8,9.
  +
]
  +
</haskell>
  +
  +
If I use <hask>:m + Data.List Control.Monad</hask> then I can demonstrate how fair the removal is:
  +
  +
<haskell>
  +
*Main Data.List Control.Monad> replicateM 1000 (getStdRandom (removeOne [1..4])) >>= return . map length . group . sort
  +
[241,255,239,265]
  +
</haskell>
  +
  +
where a perfect balance would be <hask>[250,250,250,250]</hask>

Latest revision as of 12:54, 26 June 2017


Create a random list

Generate a random list of numbers, without using the System.Random.randoms method:

import System.Random
import Data.List

main = do
    seed  <- newStdGen
    let rs = randomlist 10 seed
    print rs

randomlist :: Int -> StdGen -> [Int]
randomlist n = take n . unfoldr (Just . random)

See also the wiki page Random list

Delete an element at random

unpick and unpick' are by osfameron and are from http://osfameron.vox.com/library/post/more-random-fun.html (no explicit license)
removeOne is by Chris Kuklewicz (BSD3 licence, 2007)
import System.Random
import Control.Monad.State.Lazy
import Debug.Trace -- for removeOne' demonstration

The unpick function and its helper unpick' are strict in the entire list being operated on (forcing it all into memory at once). And IO [a] cannot lazily return any initial values.

unpick :: [a] -> IO [a]
unpick []     = undefined
unpick [x]    = return []
unpick (x:xs) = do zs <- unpick' [] [x] xs 2
                   return (reverse zs)

unpick' :: (Num p, Random p) => [t] -> [t] -> [t] -> p -> IO [t]
unpick' curr _ [] _ = return curr
unpick' curr orig (next:rest) prob
    = do r <- getStdRandom (randomR (1,prob))
         let curr' = if r == 1 then orig else (next:curr)
         unpick' curr' (next:orig) rest (prob+1)

To run in the IO Monad just use (getStdRandom . removeOne) :: [a] -> IO [a]. removeOne returns the output list lazily as soon as it has decided not to delete any element in a prefix of the input list. The resulting list is constructed efficiently, with no wasted intermediate list construction. removeOne allows any output it generates to be garbage collected, it holds no references to it.

removeOne is presented in curried form, without a binding for the RandomGen g. The StdGen is hidden inside a State monad. removeOne is designed for use with Strict.Lazy. It may not be optimal to use with Strict.Strict.

Like tail this function is partial and will produce an error if given the empty list.

removeOne :: (RandomGen g) => [a] -> g -> ([a],g)
removeOne [] = error "Cannot removeOne from empty list"
removeOne whole@(_:xs) = runState (helper whole xs 0 1) where

The laziness is needed in helper to make rest</hsak> a lazy thunk. The <hask>start list parameter to helper is a suffix of whole that has the current candidate for deletion as its head. oldIndex is the index of the current candidate for deletion in the whole list. here is a suffix of whole with the indexth element of whole as its head. randomR decides if the head of here replaces the head of start as the candidate to remove. If it does replace the old candidate then a prefix of start of length (index-oldIndex) is immediately output, counted off by prependSome.

Assert "start" is never []. Assert 0 <= oldIndex < index.

helper start [] oldIndex index = return (tail start)
helper start here@(_:ys) oldIndex index = do
   r <- State (randomR (0,index))
   if r==0 then do rest <- helper here ys index $! succ index
                   return (prependSome (index-oldIndex) start rest)
           else helper start ys oldIndex $! succ index
I assert that prependSome n xs ys == take n xs ++ ys but slightly
optimized (without depending on the compiler).  Assert n >= length xs.
prependSome :: Int -> [a] -> [a] -> [a]
prependSome 0 _ rest = rest
prependSome n (x:xs) rest = x : prependSome (pred n) xs rest
prependSome _ [] _ = error "impossible error in removeOne.prependSome"

removeOne is a tracing version for demonstration below:

removeOne' :: (Show a,RandomGen g) => [a] -> g -> ([a],g)
removeOne' [] _ = error "Cannot removeOne from empty list"
removeOne' whole@(x:xs) g = runState (helper whole xs 0 1) g where
  helper start [] oldIndex index = return (tail start)
  helper start here@(_:ys) oldIndex index = do
    r <- State (randomR (0,index))
    if r==0 then do rest <- helper here ys index $! succ index
                    let rest' = trace "." rest
                    return (prependSome (index-oldIndex) start rest')
            else do let ys' = trace "_" ys
                    helper start ys' oldIndex $! succ index

I will use removeOne to demonstrate when random decisions to drop elements are made. This also demonstrates that removeOne is lazy, returning elements as soon as the removal decision has moved on to a later element (the "." is output instead of "_").

The element after the last "." is the one actually removed, defaulting to the first element.

Since the probability of "." decreases, the average length of the run of output produced by appendSome increases as the list is processed.

*Main> getStdRandom (removeOne' [1..10])
[1.
_
_
,2,3,4.
_
_
_
_
,5,6,7,8,9.
]
*Main> getStdRandom (removeOne' [1..10])
_
_
[1,2,3.
_
_
_
_
_
_
,5,6,7,8,9,10]
*Main> getStdRandom (removeOne' [1..10])
_
_
_
_
_
_
_
_
_
[2,3,4,5,6,7,8,9,10]
*Main> getStdRandom (removeOne' [1..10])
[1.
_
_
_
_
_
_
_
_
,3,4,5,6,7,8,9,10]
*Main> getStdRandom (removeOne' [1..10])
[1.
,2.
_
_
_
_
_
_
_
,4,5,6,7,8,9,10]
*Main> getStdRandom (removeOne' [1..10])
[1.
_
_
_
_
,2,3,4,5,6.
_
_
,7,8,9.
]

If I use :m + Data.List Control.Monad then I can demonstrate how fair the removal is:

*Main Data.List Control.Monad> replicateM 1000 (getStdRandom (removeOne [1..4])) >>= return . map length . group . sort
[241,255,239,265]

where a perfect balance would be [250,250,250,250]