53 people like it.
Like the snippet!
Random Walk
Random walk on integers starting at zero. At each step, we either add or subtract one depending on a random coin flip. The code uses Seq.unfold to generate infinite sequence.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:

open System
let ran = Random()
/// Flip a coin with probability p for true
let dice p = ran.NextDouble() <= p
/// Random walk from zero stepping up and down according to dice p
let walk p =
Seq.unfold (fun z > let z = if dice p then z+1 else z1
Some (z,z)) 0
/// First n steps
let walkFor n = walk 0.5 > Seq.take n

namespace System
val ran : Random
Full name: Script.ran
Multiple items
type Random =
new : unit > Random + 1 overload
member Next : unit > int + 2 overloads
member NextBytes : buffer:byte[] > unit
member NextDouble : unit > float
Full name: System.Random

Random() : unit
Random(Seed: int) : unit
val dice : p:float > bool
Full name: Script.dice
Flip a coin with probability p for true
val p : float
Random.NextDouble() : float
val walk : p:float > seq<int>
Full name: Script.walk
Random walk from zero stepping up and down according to dice p
module Seq
from Microsoft.FSharp.Collections
val unfold : generator:('State > ('T * 'State) option) > state:'State > seq<'T>
Full name: Microsoft.FSharp.Collections.Seq.unfold
val z : int
union case Option.Some: Value: 'T > Option<'T>
val walkFor : n:int > seq<int>
Full name: Script.walkFor
First n steps
val n : int
val take : count:int > source:seq<'T> > seq<'T>
Full name: Microsoft.FSharp.Collections.Seq.take
More information