Hacker News new | past | comments | ask | show | jobs | submit login

Today I tried to write this on shell, I couldn't do it in a compact way. Are there some nice PLs for these kind of plays? Cleanest I could do was doing below in a single line via "python -c". I couldn't get it done via bc or awk.

  from random import random
  l = lambda (a, b), c: (a + c, b + 1)
  g = (1 if random()**2 + random()**2 <= 1 else 0 for i in range(10**6))
  r, t = reduce(l, g, (0.0, 0))
  print r / t * 4



  python3 -c 'from random import random; N=1000000;print(4*sum([ int(random()**2 + random()**2 <= 1.0) for i in range(N)])/N)'


F# version that fits in a tweet:

  let r,n=Random(),1e7
  let d()=(r.NextDouble()-0.5)**2.0
  [0.0..n]
  |>Seq.sumBy(function _ when d()+d()<0.25->4.0/n|_->0.0)
  |>printfn "%f"


You could try R. This ends up being a pretty straightforward one-liner:

  sum(runif(10^6, 0, 1)^2 + runif(10^6, 0, 1)^2 < 1) / 10^6 * 4




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: