Groovy web console

subscribe to the feed Subscribe
to this
site

200 coin flips

Published 2 months ago by villy
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import java.util.concurrent.atomic.AtomicInteger

def r = new Random()
def tests = (1..200).collect { r.nextBoolean() }
def runs = [new AtomicInteger(0)]
def b = tests.first()
tests.each {
    if (it == b)
        runs.last().getAndIncrement()
    else {
        runs.add(new AtomicInteger(1))
        b = !b
    }
}
println(tests)
println(runs)
runs.max()