Skip to content

Instantly share code, notes, and snippets.

@lenguyenthedat
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lenguyenthedat/e8ed6974fd5c8326972c to your computer and use it in GitHub Desktop.
Save lenguyenthedat/e8ed6974fd5c8326972c to your computer and use it in GitHub Desktop.
Randomly generated a list of n 16-bit Integers, sum up their set bits.
import System.Environment (getArgs)
import System.Random (StdGen, newStdGen, random)
import Data.Bits (popCount)
import Data.List (unfoldr)
import Data.Int (Int16)
main = do
seed <- newStdGen
args <- getArgs
let size = read $ head args
let rands = randomlist size seed
print $ sum $ map popCount rands
randomlist :: Int -> StdGen -> [Int16]
randomlist n = take n . unfoldr (Just . random)
-- $ runhaskell set-bits-int16.hs 1000000
-- 8000183
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment