[Rd] Interface for package supplied random number generator

Josef Leydold leydold at statmath.wu-wien.ac.at
Thu Aug 31 15:57:39 CEST 2006


As you probably know, there is a problem with the interface for adding uniform
random number generators in R (see by article in R News 5/2, November 2005).
There exists a mechanism called "user-supplied" that allows users of R
to run their own generator in R. However, there is no such mechanism for
package writers. Those who want to add their own generators abuse 
RNGkind(kind="user-supplied") for their purpose.
The problem with this approach is that it relies on the unique existence of the
function "user_unif_rand". When two packages are loaded that use this interface,
then at least one package is broken. If the user herself loads a shared library
that contains such a function, some of the functionalities of all the 
corresponding packages are broken.

When writing my paper I found the following packages on CRAN that abuse this
interface (meanwhile there might be more packages):


what makes the situation even more complicated is the fact that some of these
packages are loaded automaticall by other packages.
For example, package 'rlecuyer' is loaded by


To solve this problem, Stefan Theussl and I have tried to extend the mechanism
for random number generators in the following way:

- we have added a new kind of generators: "package-supplied"

- this generator kind needs functions named 'package_unif_rand', 'package_unif_init',
  'package_unif_nseed', and 'package_unif_seedloc'
  (analogously to the functions for kind "user-supplied")

- RNGkind has an additional optional argument:
    RNGkind <- function(kind = NULL, normal.kind = NULL, PACKAGE = "")
  argument 'PACKAGE' is only used for the new generator kind "package-supplied"

- similarly the function set.seed needs the optinal argument 'PACKAGE'

Since I need such an extended interface (and I assume that other package
write do also) I ask whether this extension can be included into R.
We have implemented a first experimental version of this modified interface. 
A patch against the development snapshot R-devel_2006-08-30.tar.gz is available.
However, it is a little bit too long (239 lines) to be posted here.

Of course suggestions for an alternative approach are welcome.



Josef Leydold   |  Vienna University of Economics and Business Administration
                |  Department of Statistics and Mathematics
Augasse 2-6     |  Tel.   +43 1 31336 4695
A-1090 Vienna   |  FAX    +43 1 31336 774
European Union  |  email  Josef.Leydold at statistik.wu-wien.ac.at
Alles Unglueck kam daher, dass die Denkenden nicht mehr handeln konnten,
und die Handelnden keine Zeit mehr fanden zu denken.       (Marlen Haushofer)

More information about the R-devel mailing list