Groovy web console

subscribe to the feed Subscribe
to this

Rainy Days in Austin

Published 1 month ago by Joe Wolf with tags probability
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
// these 9 characters represent our days: 2 days of rain ('R') and 7 days of dryness ('D')
def days = 'RRDDDDDDD' as List

// number of trials to run; the bigger the number, the more accurate the probability
int trials = 10000 

// counter for the number of consecutive rainy days we see
int consecutiveRainyDays = 0 

// run the trials
(1..trials).each {
    // randomize the ordering of rainy and dry days
    // if the ordering contains two Rs in a row, increment count of consecutive rainy days
    if (days.join('').contains('RR')) ++consecutiveRainyDays

// calculate and display the probability
def probability = consecutiveRainyDays / trials
println "Probability that 2 rainy days out of 9 are consecutive: $probability"