38 people like it.

Random walk

Create sequence of floating point values generated by random walk process. Functional solution using sequence expressions and yield! construct in a tail-call position.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
open System

/// Random number generator
let rnd = new Random()

/// Generates random walk starting from value 'v'
let rec randomWalk v = 
  seq { // Emit the first value of the walk
        yield v
        // Emit all values starting from new location
        yield! randomWalk (v + rnd.NextDouble()) }

// Initial call to generate random walk from value 0
randomWalk(0.0)
namespace System
val rnd : Random

Full name: Script.rnd


 Random number generator
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 randomWalk : v:float -> seq<float>

Full name: Script.randomWalk


 Generates random walk starting from value 'v'
val v : float
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
Random.NextDouble() : float
Raw view New version

More information

Link:http://fssnip.net/o
Posted:6 years ago
Author:Tomas Petricek
Tags: random walk , recursion , sequence expressions , yield