Skip to content

Instantly share code, notes, and snippets.

@BinRoot
Last active August 29, 2015 14:02
Show Gist options
  • Save BinRoot/339c21e3510347c4cd2d to your computer and use it in GitHub Desktop.
Save BinRoot/339c21e3510347c4cd2d to your computer and use it in GitHub Desktop.
import System.Random (getStdGen, randomR)
import Data.MultiSet (fromList, toOccurList)
import Data.List (maximum)
import Data.Function (on)
import Data.List (sortBy)
import Graphics.Google.Chart
import Hledger.Cli.Utils (openBrowserOn)
step xs g = (newItem : xs, newG)
where (i, newG) = pick (length xs) g
pick n = randomR (0, n-1)
newItem = if xs !! i == 0
then 1 + maximum xs
else xs !! i
steps xs _ 0 = xs
steps xs g n = steps newXs newG (n-1)
where (newXs, newG) = step xs g
main = do
g <- getStdGen
let xs = steps [0,1,2] g 5000
let freqs = sortBy (compare `on` snd) $ toOccurList $ fromList xs
putStrLn $ "There are " ++ (show.length) freqs ++ " items"
print freqs
openBrowserOn $ piChart freqs
-- limited to data values up to 4095
piChart :: [(Int, Int)] -> String
piChart freqs = chartURL $
setSize 500 400 $
setTitle ("Frequency Plot of " ++ show (length freqs) ++ " Items") $
setData (encodeDataExtended [freqVals]) $
setLabels freqIds $
newPieChart Pie2D
where freqIds = map (show.fst) freqs
freqVals = map snd freqs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment