Last active
July 16, 2017 02:43
-
-
Save neizod/6625cc1fa816b0cdb4002284e64f3634 to your computer and use it in GitHub Desktop.
R Program That Draw Histogram of Coupon's Collector Problem
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
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