Skip to content

Instantly share code, notes, and snippets.

@konr
Created January 25, 2014 04:33
Show Gist options
  • Save konr/8611923 to your computer and use it in GitHub Desktop.
Save konr/8611923 to your computer and use it in GitHub Desktop.
doge-dice.com simulator
(ns doge-dice-calculator)
(def house-edge 0.01)
(defn dice []
(rand))
(defn roll-low [chance]
(< (dice) chance))
(defn chance->payout [chance]
(-> 1 (- house-edge ) (/ chance)))
(defn play [bet chance]
(if-not (roll-low chance) 0
(* bet (chance->payout chance))))
(defn bet [wallet bet chance]
(let [res (play bet chance)]
[(not= res 0) (+ wallet (- bet) res)]))
(defn doubling-strategy
([] (doubling-strategy 0.495))
([chance] (doubling-strategy chance 1000))
([chance wallet] (doubling-strategy chance wallet 0))
([chance wallet max-games]
(loop [{:keys [wallet current-bet game] :as context}
{:wallet wallet :current-bet 1 :game 0}]
(if (or (< wallet current-bet) (and (not= max-games 0) (< max-games game))) context
(let [[won? wallet] (bet wallet current-bet chance)]
(if won? (recur {:wallet wallet :current-bet 1 :game (inc game)})
(recur {:wallet wallet :current-bet (* 2 current-bet) :game (inc game)})))))))
(defn stats [data]
{:mean (-> (apply + data) (/ (count data)))})
(defn a-thousand-times [fn]
(stats (repeatedly 1000 fn)))
;; (a-thousand-times (fn [] (float (:game (doubling-strategy 0.50 5000)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment