Skip to content

Instantly share code, notes, and snippets.

@neizod
Last active Jul 16, 2017
Embed
What would you like to do?
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