These functions allow to set some of the random number generators from randtoolbox
package to be used instead of the standard generator in the functions, which use
random numbers, for example
1 2 3 4 5 6
A character string for the RNG name.
A numeric or character vector describing a specific RNG from the
family specified by the
A number, whose value is an integer between
Arguments describing named components of the vector of parameters,
A list describing a specific RNG as created by
Random number generators provided by R extension packages are set using
RNGkind("user-supplied"). The package randtoolbox assumes that
this function is not called by the user directly and is called from
Random number generators in randtoolbox are represented at the R level by a list
containing mandatory components
name, parameters, state and possibly an
authors. The function
creates this list from the user supplied information and then runs
on this list, which initializes the generator. If the generator is initialized, then the
get.description() may be used to get the actual state of the generator,
which may be stored in an R object and used later in
put.description() to continue
the sequence of the random numbers from the point, where
was called. This may be used to generate several independent streams of random numbers
generated by different generators.
parameters is a character or a numeric vector, whose structure
is different for different types of the generators. This vector may be passed
set.generator(), if it is prepared before its call, however, it is
also possible to pass its named components via the
... parametr of
set.generator() and the vector
parameters is created internally.
If the vector
parameters is not supplied and the arguments in
are not sufficient to create it, an error message is produced.
Parameters for the linear congruential generators (
are integers represented either as a character or a numeric vector. The
Parameters for the WELL generators is a character vector with components
The number of bits in the internal state. Possible values are 512, 521, 607, 800, 1024, 19937, 21701, 23209, 44497.
The version letter "a", "b", or "c" to be appended to the order.
The concatenation of
version should belong to
"512a", "521a", "521b", "607a", "607b", "800a", "800b", "1024a", "1024b",
"19937a", "19937b", "19937c", "21701a", "23209a", "23209b", "44497a", "44497b".
When order and version are specified in
... parametr of
then the parameter
order is optional and if missing, it is assumed that the
version contains also the number of bits in the internal state
and the combination belongs to the list above.
Parameters for the Mersenne Twister 2002 is a character vector with components
Either "init2002" or "array2002". The initialization to be used.
Either 53 or 32. The number of random bits in each number.
set.generator() with the parameter
get.description() return the list describing a generator.
put.description() with the parameter
only.dsc=TRUE (the default)
Petr Savicky and Christophe Dutang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#set WELL19937a set.generator("WELL", version="19937a", seed=12345) runif(5) #Store the current state and generate 10 random numbers storedState <- get.description() x <- runif(10) #Park Miller congruential generator set.generator(name="congruRand", mod=2^31-1, mult=16807, incr=0, seed=12345) runif(5) setSeed(12345) congruRand(5, dim=1, mod=2^31-1, mult=16807, incr=0) # the Knuth Lewis RNG set.generator(name="congruRand", mod="4294967296", mult="1664525", incr="1013904223", seed=1) runif(5) setSeed(1) congruRand(5, dim=1, mod=4294967296, mult=1664525, incr=1013904223) #Restore the generator from storedState and regenerate the same numbers put.description(storedState) x == runif(10) # generate the same random numbers as in Matlab set.generator("MersenneTwister", initialization="init2002", resolution=53, seed=12345) runif(5) #  0.9296161 0.3163756 0.1839188 0.2045603 0.5677250 # Matlab commands rand('twister', 12345); rand(1, 5) generate the same numbers, # which in short format are 0.9296 0.3164 0.1839 0.2046 0.5677 #Restore the original R setting set.generator("default") RNGkind()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.