Skip to content

Instantly share code, notes, and snippets.

@TheSeamau5
Created April 20, 2016 00:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save TheSeamau5/266f0c80a06e8816d5f99d3b743513d1 to your computer and use it in GitHub Desktop.
Make uniform distribution from 3 dice
import Random exposing (Generator, Seed)
import Signal exposing (Signal)
import Graphics.Element exposing (Element, show, flow, down)
import Time exposing (Time)
import String
import List
die : Generator Int
die =
Random.float 0 6
|> Random.map ceiling
dice2 : Generator Int
dice2 =
Random.map2 (\x y -> (6 * x + y - 1) // 6) die die
dice3 : Generator Int
dice3 =
Random.map3 (\x y z -> (36 * x + 6 * y + z - 7) // 36) die die die
countSimilar : List Int -> List { value : Int, count : Int }
countSimilar list =
let
partitionedList =
list
|> List.sort
|> partitionSimilar
countList list =
case list of
[] ->
{ value = 0, count = 0 }
x :: xs ->
{ value = x, count = List.length list }
in
List.map countList partitionedList
partitionSimilar : List a -> List (List a)
partitionSimilar list =
case list of
[] ->
[]
x :: xs ->
let
(similar, distinct) =
List.partition (\y -> x == y) xs
in
[x :: similar] ++ partitionSimilar distinct
viewCount : { value : Int, count : Int } -> Element
viewCount { value, count } =
let
asterisks =
List.repeat count "*"
|> String.concat
in
toString value ++ ": " ++ asterisks
|> show
viewManyCounts : List { value : Int, count : Int } -> Element
viewManyCounts list =
list
|> List.map viewCount
|> flow down
viewList : List Int -> Element
viewList =
countSimilar >> viewManyCounts
generateN : Int -> Generator a -> Seed -> List a
generateN n generator seed =
if
n <= 0
then
[]
else
let
(value, nextSeed) = Random.generate generator seed
in
value :: generateN (n - 1) generator nextSeed
currentTime : Signal Time
currentTime =
()
|> Signal.constant
|> Time.timestamp
|> Signal.map fst
run : Time -> Element
run time =
time
|> round
|> Random.initialSeed
|> generateN 400 dice3
|> viewList
main
= Signal.map run currentTime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment