Skip to content

Instantly share code, notes, and snippets.

@mccanne
Last active August 29, 2015 14:10
Show Gist options
  • Save mccanne/d73593dcfb87880d6e91 to your computer and use it in GitHub Desktop.
Save mccanne/d73593dcfb87880d6e91 to your computer and use it in GitHub Desktop.
Multiple dice games

A simple example where we run multiple dice games, illustrating how modularity and subgraphs are helpful

sub dice_rolls(n) {
function roll() {
return Math.ceil(6 * Math.random());
}
function toss_dice() {
return roll() + roll();
}
emitter -limit n -start 0 | put throw = toss_dice()
}
sub frequency_table(total, field) {
reduce total=count() by field | sort field
}
sub histogram(title, what) {
frequency_table -total 'total' -field what |
@barchart -title title -categoryField what -valueField 'total'
}
sub throw_dice(n) {
const title = String.concat("Dice Rolls - ", Number.toString(n));
dice_rolls -n n | histogram -title title -what 'throw'
}
// try it a bunch of different runs
throw_dice -n 10 ;
throw_dice -n 100 ;
throw_dice -n 1000 ;
throw_dice -n 10000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment