[Haskell-cafe] approximating pi

jerzy.karczmarczuk at info.unicaen.fr jerzy.karczmarczuk at info.unicaen.fr
Sun Apr 27 20:54:39 EDT 2008


Don Stewart writes: 

> Ry Dahl:
>> By picking points randomly from a square one can calculate pi.
...
>> What is the best way to express this algorithm in Haskell?
> 
> Using a random generator, such as System.Random or
> System.Random.Mersenne, generate random numbers in your range,
> testing if they're inside the circle, and loop until your limit
> condition is reached. 
> 
> The full program is about 10 lines or so, so shouldn't be too hard to
> work out, once you've worked out how to generate Doubles from
> System.Random.random

Ry Dahl seems to know Ruby. Ruby has also comprehensions. So, why not use
'randoms' to generate an infinite list of them, 'take' some N, and then
'sum' 1 on randoms filtered by the circle condition. I think that you
won't need full 10 lines of code... 

Jerzy Karczmarczuk 




More information about the Haskell-Cafe mailing list