#r "nuget: Plotly.NET.Interactive"
#r "nuget: Plotly.NET.ImageExport"
open System
open System.Runtime.CompilerServices
open Plotly.NET
open Plotly.NET.ImageExport
open Plotly.NET.LayoutObjects
let random = Random()
let staticConfig = Config.init(
StaticPlot = true
)
type Chart with
static member Display (chart: GenericChart.GenericChart) =
match GenericChart.tryGetLayoutSize chart with
| Some width, Some height ->
chart
|> Chart.withConfig staticConfig
|> Chart.toSVGString(Width = width, Height = height)
|> (fun chart -> chart.DisplayAs "text/html")
| _ ->
chart
|> Chart.withConfig staticConfig
|> Chart.toSVGString()
|> (fun chart -> chart.DisplayAs "text/html")
type RollOfDice =
| One = 0
| Two = 1
| Three = 2
| Four = 3
| Five = 4
| Six = 5
let rollLoadedDice () =
let rec loadedRoll numberOfAttempts =
let roll = enum<RollOfDice>(random.Next 6)
match roll with
| RollOfDice.Four when numberOfAttempts <= 3 -> loadedRoll (numberOfAttempts + 1)
| x -> enum<RollOfDice>(random.Next 6)
loadedRoll 0
let rollLoadedDice () =
match random.NextDouble() with
| x when 0.000 <= x && x < 0.080 -> RollOfDice.Four
| x when 0.080 <= x && x < 0.264 -> RollOfDice.One
| x when 0.264 <= x && x < 0.448 -> RollOfDice.Two
| x when 0.448 <= x && x < 0.632 -> RollOfDice.Three
| x when 0.632 <= x && x < 0.816 -> RollOfDice.Five
| x (* when 0.816 <= x < 1.0 *) -> RollOfDice.Six
let rollFairDice () =
enum<RollOfDice>(random.Next 6)
rollLoadedDice()
rollFairDice()
let generateRawData (roll: Unit -> RollOfDice) numberOfRolls =
[for _ in [1..numberOfRolls] -> roll()]
let chartSize = 350
let layoutStyle = Layout.init(
Margin = Margin.init(20, 20, 20, 20),
BarGap = 0.1)
let prepareDataForHistogram (rollsOfDice: RollOfDice list) =
rollsOfDice
|> List.sortBy int
|> List.map (sprintf "%A")
generateRawData rollLoadedDice 500
|> prepareDataForHistogram
|> Chart.Histogram
|> Chart.withSize (chartSize, chartSize)
|> Chart.withXAxisStyle (ShowGrid = true)
|> Chart.withLayout layoutStyle
|> Chart.withMarkerStyle(Color = Color.fromKeyword DarkCyan)
|> Chart.Display
generateRawData rollFairDice 50_000
|> prepareDataForHistogram
|> Chart.Histogram
|> Chart.withSize (chartSize, chartSize)
|> Chart.withXAxisStyle (ShowGrid = true)
|> Chart.withLayout layoutStyle
|> Chart.withMarkerStyle(Color = Color.fromKeyword DarkCyan)
|> Chart.Display