Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import Html exposing (Html, div, text, button)
import Html.App exposing (beginnerProgram)
import Html.Events exposing (onClick)
import Random exposing (Seed, initialSeed, step, float, bool)
-- Start with initial probability
-- For every miss :
-- p = p * 0.9
-- For every hit :
-- p = p + (1 - p) * 0.1
-- p = p * 0.8 + 0.2
-- State
type alias State =
{ probability : Float
, seed : Seed
}
-- Initialize with base probability
initState : Float -> State
initState p =
{ probability = clamp 0 1 p
, seed = initialSeed 42
}
-- Get next result
next : State -> (State, Bool)
next { probability, seed } =
let
genProb = float 0 1
(p, seed') = step genProb seed
hit = p < probability
prob =
if hit
then
probability + (1 - probability) * 0.1
else
0.9 * probability
nextState =
{ probability = prob
, seed = seed'
}
in
(nextState, hit)
type alias Model =
{ state : State
, hits : Int
, misses : Int
}
init : Float -> Model
init p =
{ state = initState p
, hits = 0
, misses = 0
}
update : Model -> Model
update { state, hits, misses } =
let
(nextState, hit) = next state
hits' =
if hit
then
hits + 1
else
hits
misses' =
if hit
then
misses
else
misses + 1
in
{ state = nextState
, hits = hits'
, misses = misses'
}
view : Model -> Html ()
view { state, hits, misses } =
div
[]
[ div []
[ text ("Probability: " ++ toString state.probability) ]
, div []
[ text ("Hits: " ++ toString hits)]
, div []
[ text ("Misses: " ++ toString misses)]
, button
[ onClick () ]
[ text "Next" ]
]
main =
beginnerProgram
{ model = init 0.19
, view = view
, update = always update
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment