Last active
June 26, 2018 20:48
-
-
Save primaryobjects/02b804ecaddcad7410140600bf12020b to your computer and use it in GitHub Desktop.
[2018-06-18] Challenge #364 [Easy] Create a Dice Roller https://www.reddit.com/r/dailyprogrammer/comments/8s0cy1/20180618_challenge_364_easy_create_a_dice_roller/ Demo at http://rextester.com/WACS34508
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
roll <- function(input) { | |
parts <- as.numeric(unlist(strsplit(input, 'd'))) | |
sum(sample(1:parts[2], parts[1], replace=T)) | |
} |
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
input <- c('5d12', '6d4', '1d2', '1d8', '3d6', '4d20', '100d100') | |
result <- sapply(input, function(value) { | |
sum(roll(value)) | |
}) | |
print(data.frame(input=input, value=result)) | |
# Generate a histogram plot to visualize the spread of dice values as random. | |
r <- '4d20' | |
x <- sapply(1:10000, function(i) { roll(r) }) | |
df <- data.frame(index=seq_along(x), value=x) | |
hist(x, main=paste('Histogram of', r)) | |
ggplot(data=df, aes(df$value)) + | |
geom_histogram(col="red", aes(fill=..count..)) + | |
scale_fill_gradient("Count", low = "blue", high = "purple") + | |
xlab('Roll') + | |
ylab('Frequency') + | |
ggtitle(paste('Histogram of', r)) |
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
input value | |
5d12 5d12 42 | |
6d4 6d4 11 | |
1d2 1d2 2 | |
1d8 1d8 2 | |
3d6 3d6 11 | |
4d20 4d20 25 | |
100d100 100d100 5215 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment