Groovy web console

subscribe to the feed Subscribe
to this
site

two candles one cake (Numberphile)

Published 3 months ago by @seewhatyoudontsee with tags math riddle numberphile
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
def attempts = 10000000
def successes = 0
def failures = 0

for (def i = 0; i < attempts; i++) {
  def (c1, c2, k) = [Math.random(), Math.random(), Math.random()]
  if (c1 < k && k < c2) successes++
  else if (c2 < k && k < c1) successes++
  else failures++
}

def prob = successes / (double) attempts

println "successes: $successes, failures $failures, prob $prob"