Skip to content

Instantly share code, notes, and snippets.

@nuald nuald/gen.R
Created Aug 2, 2016

Embed
What would you like to do?
Generate series for a breach detection sample
#!/usr/bin/env Rscript
suppressPackageStartupMessages(library("argparse"))
parser <- ArgumentParser(
description="Generate series for a breach detection sample"
)
parser$add_argument("-N", "--checks", type="integer", default=5,
help="Number of checks [default %(default)s]",
metavar="checks")
parser$add_argument("-M", "--attempts", type="integer", default=6,
help="Max number of attempts [default %(default)s]",
metavar="attempts")
parser$add_argument("-T", "--period", type="integer", default=60,
help="Max time period for a session [default %(default)s]",
metavar="period")
parser$add_argument("-K", "--series", type="integer", default=1000,
help="Number of items in series [default %(default)s]",
metavar="series")
parser$add_argument("-I", "--samples", type="integer", default=1000,
help="Number of samples [default %(default)s]",
metavar="samples")
parser$add_argument("-O", "--out", type="character", default="stdout",
help="Output file [default %(default)s]",
metavar="out")
args <- parser$parse_args()
fmt <- list(fmt="(%02d,%02d)")
legitimate.core <- mapply(sprintf, MoreArgs=fmt,
sample(0:(args$attempts / 2), args$series, replace=T),
sample(0:(args$period / 2), args$series, replace=T),
SIMPLIFY=F
)
malicious.core <- mapply(sprintf, MoreArgs=fmt,
sample((args$attempts / 2):args$attempts, args$series, replace=T),
sample((args$attempts / 2):args$period, args$series, replace=T),
SIMPLIFY=F
)
abnormal.core <- mapply(sprintf, MoreArgs=fmt,
sample(0:args$attempts, args$series, replace=T),
sample(0:args$period, args$series, replace=T),
SIMPLIFY=F
)
legitimate.series <- c(
sample(legitimate.core, args$series / 2, replace=T),
sample(malicious.core, args$series / 4, replace=T),
sample(abnormal.core, args$series / 4, replace=T)
)
malicious.series <- c(
sample(legitimate.core, args$series / 4, replace=T),
sample(malicious.core, args$series / 2, replace=T),
sample(abnormal.core, args$series / 4, replace=T)
)
abnormal.series <- c(
sample(legitimate.core, args$series / 4, replace=T),
sample(malicious.core, args$series / 4, replace=T),
sample(abnormal.core, args$series / 2, replace=T)
)
GenUser <- function() {
user.class.num <- sample(1:3, 1)
user.class <- switch(user.class.num, "legitimate", "malicious", "abnormal")
series <- switch(user.class.num,
sample(legitimate.series, args$checks, replace=T),
sample(malicious.series, args$checks, replace=T),
sample(abnormal.series, args$checks, replace=T)
)
sprintf("%s\t%s", user.class, paste(series, collapse=" "))
}
users <- replicate(args$samples, GenUser())
cat(
paste(users, collapse="\n"),
file=ifelse(args$out == "stdout", "", args$out)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.