Skip to content

Instantly share code, notes, and snippets.

@neizod
Last active July 16, 2017 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neizod/6625cc1fa816b0cdb4002284e64f3634 to your computer and use it in GitHub Desktop.
Save neizod/6625cc1fa816b0cdb4002284e64f3634 to your computer and use it in GitHub Desktop.
R Program That Draw Histogram of Coupon's Collector Problem
random.int <- function(n) { sample.int(n, 1) }
random.coupon <- function(...) {
count <- 0
have.coupon <- logical(...)
while (!all(have.coupon)) {
have.coupon[random.int(...)] <- TRUE
count <- count + 1
}
count
}
sample.coupon <- function(n, size=10*n) {
result <- NULL
for (i in 1:size) {
result <- c(result, random.coupon(n))
}
result
}
if (!interactive()) {
args <- commandArgs(TRUE)
if (length(args) == 1) {
filename <- "sample-coupon.png"
} else if (length(args) == 2) {
filename <- args[2]
} else {
stop("wrong number of argument!")
}
png(filename, width=800, height=800)
arg.list <- list(x=as.numeric(args[1]))
do.call(hist, list(substitute(sample.coupon(x), arg.list)))
dev.off()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment