Skip to content

Random Seeds (and seeding the GSL library)

Vince Buffalo edited this page May 4, 2017 · 4 revisions

Creating random seeds without replacement for simulations

I use this to pass seeds to GNU parallel for parallel simulations (thanks Kevin Thornton for some tips with sample.int()). Here is the command for OS X:

seeds() {
    Rscript -e "options(warn=2); args <- commandArgs(TRUE); set.seed(as.integer(args[1])); cat(paste(sample.int(.Machine\$integer.max, args[2], replace=FALSE), collapse='\\\\n')); cat('\\\\n')" $1 $2
}

for Linux, we don't need quite as many escapes (yay!):

seeds() {
    Rscript -e "options(warn=2); args <- commandArgs(TRUE); set.seed(as.integer(args[1])); cat(paste(sample.int(.Machine\$integer.max, args[2], replace=FALSE), collapse='\\n')); cat('\\n')" $1 $2
}

Usage: seeds seed howmany where seed is the seed to provide to R and howmany is how many seeds to create. No duplicate seeds will be used (replace=FALSE).

GSL Seeds

The GSL has a terrific interface for generating psuedo-random numbers (and it's faster than C++'s random library).

Approaches

Seeding

export GSL_RNG_SEED=$(head -1 /dev/urandom | od -N 10 | awk '{print $2}') && your_program
Clone this wiki locally