Skip to content

Instantly share code, notes, and snippets.

@ear
Last active November 22, 2017 16:46
Show Gist options
  • Save ear/e299784e57e3e75ac9c8460f0ccbedcc to your computer and use it in GitHub Desktop.
Save ear/e299784e57e3e75ac9c8460f0ccbedcc to your computer and use it in GitHub Desktop.
min (unif(0,1),unif(0,1)) ~~ ??
module Main where
import Data.List (splitAt, foldl')
import Control.Monad (forM_)
import System.Random (randomRs, getStdGen)
import System.Environment (getArgs)
import qualified Data.Map.Strict as M
main = do
[size, digits, columns] <- map read <$> getArgs :: IO [Int]
rs <- take (size*2) <$> (randomRs (0,1) <$> getStdGen) :: IO [Double]
let (xs,ys) = splitAt size rs
zs = map (truncate . (* (fromIntegral digits))) (zipWith min xs ys)
cs = foldl' (\m x -> M.insertWith (const succ) x 1 m) (M.fromList []) zs
cols count = round $ (fromIntegral count) / (fromIntegral $ maximum $ M.elems cs) * (fromIntegral columns)
forM_ (M.toList cs) $ \(v,count) ->
putStrLn $ show v ++ " " ++ replicate (cols count) '*'
--------
$ ./A 10000 30 100
0 ****************************************************************************************************
1 ****************************************************************************************************
2 **********************************************************************************************
3 ***************************************************************************************
4 ************************************************************************************
5 **********************************************************************************
6 *********************************************************************************
7 ***********************************************************************
8 *************************************************************************
9 ******************************************************************
10 ***************************************************************
11 *****************************************************************
12 *****************************************************
13 *****************************************************
14 ****************************************************
15 ***************************************************
16 **********************************************
17 ******************************************
18 ******************************************
19 ********************************
20 *******************************
21 ****************************
22 ************************
23 ***********************
24 *******************
25 *****************
26 **********
27 *********
28 ****
29 **
$ time ./A 1000000 30 100
0 ****************************************************************************************************
1 *************************************************************************************************
2 **********************************************************************************************
3 ******************************************************************************************
4 ***************************************************************************************
5 ***********************************************************************************
6 ********************************************************************************
7 *****************************************************************************
8 *************************************************************************
9 *********************************************************************
10 *******************************************************************
11 **************************************************************
12 ************************************************************
13 ********************************************************
14 *****************************************************
15 **************************************************
16 ***********************************************
17 ******************************************
18 ***************************************
19 ************************************
20 ********************************
21 *****************************
22 *************************
23 **********************
24 *******************
25 ***************
26 ************
27 ********
28 *****
29 **
real 0m3.824s
user 0m3.280s
sys 0m0.227s
$ time ./A 10000000 30 100
0 ****************************************************************************************************
1 *************************************************************************************************
2 *********************************************************************************************
3 ******************************************************************************************
4 ***************************************************************************************
5 ***********************************************************************************
6 ********************************************************************************
7 ****************************************************************************
8 *************************************************************************
9 **********************************************************************
10 ******************************************************************
11 ***************************************************************
12 ***********************************************************
13 ********************************************************
14 *****************************************************
15 *************************************************
16 **********************************************
17 ******************************************
18 ***************************************
19 ************************************
20 ********************************
21 *****************************
22 **************************
23 **********************
24 *******************
25 ***************
26 ************
27 ********
28 *****
29 **
real 0m39.969s
user 0m35.027s
sys 0m2.142s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment