Select random element from a set, faster than linear time (Haskell)

前端 未结 8 1667
清酒与你
清酒与你 2021-01-17 11:38

I\'d like to create this function, which selects a random element from a Set:

randElem :: (RandomGen g) => Set a -> g -> (a, g)

Si

8条回答
  •  佛祖请我去吃肉
    2021-01-17 12:11

    Data.Map has an indexing function (elemAt), so use this:

    import qualified Data.Map as M
    import Data.Map(member, size, empty)
    import System.Random
    
    type Set a = M.Map a ()
    
    insert :: (Ord a) => a -> Set a -> Set a
    insert a = M.insert a ()
    
    fromList :: Ord a => [a] -> Set a
    fromList = M.fromList . flip zip (repeat ())
    
    elemAt i = fst . M.elemAt i
    
    randElem :: (RandomGen g) => Set a -> g -> (a, g)
    randElem s g = (elemAt n s, g')
        where (n, g') = randomR (0, size s - 1) g
    

    And you have something quite compatible with Data.Set (with respect to interface and performance) that also has a log(n) indexing function and the randElem function you requested.

    Note that randElem is log(n) (and it's probably the fastest implementation you can get with this complexity), and all the other functions have the same complexity as in Data.Set. Let me know if you need any other specific functions from the Set API and I will add them.

提交回复
热议问题