Package ‘rngSetSeed’ February 15, 2013

Type Package

Title Initialization of the R base random number generator Mersenne-Twister with a vector of an arbitrary length.

Version 0.2-3

Date 2012-03-08

Author Petr Savicky

Maintainer Petr Savicky

Description The package provides a function setVectorSeed(vseed). Its argument is a vector of arbitrary nonzero length, including length 1, whose components have integer values from [0, 2^32-1] and which is used to initialize the R base Mersenne-Twister random number generator. In order to guarantee that different input vectors yield unrelated streams of random numbers, a random number generator based on AES (Advanced Standard) is used for the transformation of vseed into the initial state of Mersenne-Twister. Due to the use of AES, the function provides a better alternative to R base function set.seed() even if vseed is a single integer. Initializing random number stream with a vector including a reproducible index of an R process allows to use reproducible and unrelated streams of random numbers in several independent R processes. The package contains test scripts, which verify its installation by comparing the output with a stored precomputed one and by comparing the encryption provided by C-level AES with an implementation of AES at the R-level, which is included for test purposes. See CRAN package ‘‘randaes’’ for a random number generator based purely on AES.

License GPL-2

LazyLoad yes

Repository CRAN

1 2 rngSetSeed-package

Date/Publication 2012-03-08 14:22:01

NeedsCompilation yes

R topics documented:

rngSetSeed-package ...... 2 getVectorSeed ...... 3 setVectorSeed ...... 4

Index 6

rngSetSeed-package Initialization of Mersenne-Twister random number generator using AES.

Description

The package provides function setVectorSeed(vseed), which allows to initialize the R base Mersenne-Twister random number generator using a numeric vector vseed of arbitrary nonzero length, including length 1. In order to guarantee that different seeding vectors yield unrelated streams of random numbers, the transformation of vseed into the initial state of Mersenne-Twister is computed by Fortuna random number generator using Advanced Encryption Standard (AES) as the underlying . Initializing using a vector seed may be used to provide reproducible and unrelated streams of ran- dom numbers in independent R processes, each of which knows its reproducible index (not a pro- cess number). For this purpose, use something like setVectorSeed(c(base.seed, ind, i, j)), where base.seed is a global seed for a group of R processes, ind is an index of the R process and i, j may be loop variables of nested for loops, for each repetition of which we want to start a new stream of random numbers. The directory "rngSetSeed/tests" contains tests, which

(1) compare the generated random numbers to stored precomuted ones, (2) compare the initial states obtained using AES in C-level functions with initial states obtained using a (slow) implementation of AES in R-level functions included for test purposes.

See Also

setVectorSeed, getVectorSeed. getVectorSeed 3

getVectorSeed Generates a random integer vector of a specified length using AES.

Description Transforms an input vector of an arbitrary length to a random integer vector of a specified length using Advanced Encryption Standard (AES) block cipher. If the output length is 624, then it is suitable as an initial state of Mersenne-Twister random number generator.

Usage getVectorSeed(vseed, m)

Arguments vseed Numeric vector of an arbitrary nonzero length, whose components have integer values from [0, 2^32 - 1]. m Numeric, the length of the required output integer vector.

Details The vector vseed is first replaced by c(vseed, length(vseed)) in order to guarantee that if vseed1 is the first part of vseed2, but they have a different length, then the outputs are unre- lated. If the length of the resulting vector is not divisible by 8, the vector is padded by zeros to the nearest larger length divisible by 8. The resulting vector is splitted into k blocks of length 8 and these blocks are used as 256-bit keys in AES to encrypt k different counter sequences of length mm == ceiling(m/4). The encrypted values from all k rounds are combined by XOR to a single sequence of mm values, each of which is a sequence of 16 bytes. These sequences are splitted into subsequences of 4 bytes. Each of these 4*mm subsequences is combined to one 32-bit integer in an endianness independent way. The first m of the obtained integers form the output. The first round, which is the only round, if length(vseed) <= 7, is Fortuna random number gen- erator using AES as described at http://en.wikipedia.org/wiki/Fortuna_(PRNG) with a specified by the user. If length(vseed) >= 8, the algorithm uses XOR of the outputs of several Fortuna generators with keys formed from disjoint parts of the input vector and disjoint counter sequences. If m1 < m2, then the first m1 components of getVectorSeed(vseed, m2) and getVectorSeed(vseed, m1) are equal.

Value Vector of length m of integer type. Its components are from [-2^31 + 1, 2^31 - 1] or NA, which represents -2^31. These values are interpreted as unsigned integers in [0, 2^32 - 1] modulo 2^32. If m == 624, the output vector is suitable as the initial state of Mersenne-Twister to be copied into .Random.seed[3:626]. 4 setVectorSeed

References http://en.wikipedia.org/wiki/Advanced_Encryption_Standard, http://en.wikipedia.org/ wiki/Fortuna_(PRNG).

See Also setVectorSeed

Examples getVectorSeed(0, 3) == c(1418754292, -1328910215, -1268802982) getVectorSeed(c(0, 1), 3) == c(1456767579, 1606355844, -1560283904) getVectorSeed(c(0, 1, 2), 3) == c(-1265102184, -974528510, 163341403) getVectorSeed(c(0, 1, 2), 3) == getVectorSeed(c(0, 1, 2), 33)[1:3]

setVectorSeed Initializes Mersenne-Twister random number generator with a vector of an arbitrary nonzero length.

Description Initializes Mersenne-Twister random number generator with a numeric vector of arbitrary nonzero length, including length 1, whose components are interpreted as 32-bit integers. In order to guaran- tee that different input vectors yield unrelated streams of random numbers, Fortuna random number generator using Advanced Encryption Standard (AES) is used for the transformation of vseed to the initial state of Mersenne-Twister. See getVectorSeed for more detail of the algorithm.

Usage setVectorSeed(vseed)

Arguments vseed Numeric vector of arbitrary nonzero length with integer values from [0, 2^32-1].

Details The function calls RNGkind("Mersenne-Twister") and then replaces its state with getVectorSeed(vseed) as an initial state. See getVectorSeed for the description of the algorithm computing the initializa- tion of length 624 from vseed.

Value NULL invisibly.

See Also getVectorSeed. setVectorSeed 5

Examples setVectorSeed(1) runif(5) setVectorSeed(c(1, 0)) runif(5) setVectorSeed(1) runif(5) Index

getVectorSeed, 2,3, 4 rngSetSeed (rngSetSeed-package),2 rngSetSeed-package,2 setVectorSeed, 2, 4,4

6