Modelling dice rolls in F# -


I'm doing some Monte Carlo simulations using the foos on the dice roll and I have a roll of a dice To represent the list of dice rolls, I want to be able to model dice roll instead of integer as a type of integer to represent the list of dice rolls. I am using only 6 sided dice and I have a kind of choice which is some of the nickname / synonyms and some discriminating association or qualities of enum.

I would like to see my type in this

  1. I would like to treat type as an integer so that I can do max / minute / yoga / fold on lists. / Li>
  2. I should be able to specify an integer value.
  3. I must be compelled with type 1-6 type, so that there is no dice roll, there is no dice roll of a zero and seven.

I have tried to combine the types listed above and everyone likes to draw some (which can understand myself rather than my use and understanding).

I'm doing this for fun (not for profit) but would like to see an answer, as I can imagine using it in more serious data modeling, this is a trivial example.

It seems that in a steady way, a discriminating union can handle whatever you want

  open system type D6 = | One | Two Three | Four Five Six members themselves Value = Match with self. One - & gt; 1 | Two - & gt; 2 | Three - & gt; 3 | Four - & gt; 4 | Five - & gt; 5 | Six - & gt; 6 override self Matching yourself with toasting () =. One - & gt; "A" | Two - & gt; "Two" | Three - & gt; "Three" | Four - & gt; "Four" | Five - & gt; "Five" | Six - & gt; Create "Six" static members (num: int) = matching digits | 1 - & gt; One | 2 - & gt; Two 3 - & gt; Three | 4 - & gt; Four 5 - & gt; Five 6 - & gt; Six | _ - & gt; Failed "D6 could not create,% d range is 1-6" not in num static member Inline Roll () = Random (). Next (1,7) & Gt; D6.Create  

Calling D6.Roll () will generate a random random roll

& gt; D6.Roll () ;; This value is: D6 = four

& gt; D6.Roll () ;; This value is: D6 = six

& gt; D6.Roll () ;; This value is: D6 = 2

& gt; D6.Roll () ;; You can make it stable: D6 = Five

You can create stable members and operators that make your D6 easier to combine

 < Code> Add stable member inline (a: D6) (B: ​​D6) (B: ​​D6) = A.WW + B Value Static Member Inline (+) (A, B) = D6 Ad stable member inline substrate (A: D6) (B: ​​D6) = A. Vel - B.Value stable member inline (-) (A, B) = D6.Subtract ab  

and static members who make it easy to make them from the list of inputs

List from stable members (numls: int list) = numls | & Gt; List.map D6.Create

Comparison exits the box like this

D6.One & gt; D6.Two ;; This value is: bool = false

D6.One & lt; D6.Two ;; Unique scenes make it easy to generate random input

  let rollgen = give rnd = random () give REC generator () = cache {yield rd. Next (1,7) yield! General ()} General ()  

& gt; RollGen ;;

Which map can be done to make an infinite random sequence of D6

  let's d6Gen = rollGen | & Gt; Seq.map D6.Create  

& gt; D6Gen ;; If you want to draw a constant list of input values, then you can reuse it from random infinite sequences, such as prior> roll list num = rollGen | & Gt; Seq.take number | & Gt; List. OFS Deck D6 List Number = D6 Gene | & Gt; Seq.take number | & Gt; List.ofSeq ;;

let's _20rolls = rollList 20 ;;

& gt; Val_20rolls: int list = [3; 4; 2; 3; 5; 6; 4; 6; 6; 6; 5; 3; 4; 3; 2; 1; 2; 5; 3; 6] *)

let's _30d6 = d6 list 30 ;;

& gt; Val _30d6: D6 list = [two; Six; One; three; two; three; One; One; Six; Six; Four; Four; three; Four; One; Five; three; Four; Four; Four; three; two; Six; Four; One; three; One; Five; two; Two]


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -