C# code
public IEnumerable<int> Passion(int a, int n)
{
for (int i = 0; i < n; i++)
{
var L = Math.Exp(-a);
var k = 0;
var p = 1.0;
do
{
k++;
p *= rand.NextDouble();
}
while (p > L);
yield return k - 1;
}
}
F# code
open System
let rand = Random()
let passion (a:int) =
let getOneSample (a:int) =
let L = exp(float (-a) )
Seq.unfold (fun p ->
if p > L then Some (1, p*rand.NextDouble())
else None) 1.
|> Seq.length
|> fun len -> len - 1
Seq.initInfinite (fun _ -> getOneSample a)
passion 5
|> Seq.take 100
|> Seq.toList
2 comments:
it's only me...or the c# version looks more readable?..
That, and the two versions of the function don't take the same number of arguments...
Post a Comment