A simple example where we run multiple dice games, illustrating how modularity and subgraphs are helpful
Last active
August 29, 2015 14:10
-
-
Save mccanne/d73593dcfb87880d6e91 to your computer and use it in GitHub Desktop.
Multiple dice games
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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