Skip to content

Instantly share code, notes, and snippets.

@jamescookie
Created May 8, 2015 12:54
Show Gist options
  • Save jamescookie/d969dcf2c1a5bab9e716 to your computer and use it in GitHub Desktop.
Save jamescookie/d969dcf2c1a5bab9e716 to your computer and use it in GitHub Desktop.
import java.util.Random
Random rand = new Random()
def colours = ['blue', 'green', 'red', 'yellow']
def likihood = [blue: 25, green: 22, red: 22, yellow: 5]
def views = [blue: 1, green: 1, red: 1, yellow: 1]
def clicks = [blue: 1, green: 1, red: 1, yellow: 1]
def percentages
for (def i = 0; i < 10000; i++) {
def colour
if (rand.nextInt(100) < 10) {
colour = colours[rand.nextInt(colours.size())]
} else {
percentages = colours.collectEntries {
[(it): clicks[it]/views[it]*100]
}
percentages = percentages.sort {a, b -> b.value <=> a.value}
colour = percentages.find {true}.key
}
views[colour] = views[colour] + 1
if (rand.nextInt(100) < likihood[colour]) {
clicks[colour] = clicks[colour] + 1
}
}
println clicks.sort {a, b -> b.value <=> a.value}.find{true}
println percentages
println views
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment