11# File src/library/base/R/RNG.R
22# Part of the R package, https://www.R-project.org
33#
4- # Copyright (C) 1995-2014 The R Core Team
4+ # Copyright (C) 1995-2019 The R Core Team
55#
66# This program is free software; you can redistribute it and/or modify
77# it under the terms of the GNU General Public License as published by
2121# # The available kinds are in
2222# # ../../../include/Random.h and ../../../main/RNG.c [RNG_Table]
2323# #
24- RNGkind <- function (kind = NULL , normal.kind = NULL )
24+ RNGkind <- function (kind = NULL , normal.kind = NULL , sample.kind = NULL )
2525{
2626 kinds <- c(" Wichmann-Hill" , " Marsaglia-Multicarry" , " Super-Duper" ,
2727 " Mersenne-Twister" , " Knuth-TAOCP" , " user-supplied" ,
2828 " Knuth-TAOCP-2002" , " L'Ecuyer-CMRG" , " default" )
2929 n.kinds <- c(" Buggy Kinderman-Ramage" , " Ahrens-Dieter" , " Box-Muller" ,
3030 " user-supplied" , " Inversion" , " Kinderman-Ramage" ,
3131 " default" )
32+ s.kinds <- c(" Rounding" , " Rejection" , " default" )
3233 do.set <- length(kind ) > 0L
3334 if (do.set ) {
3435 if (! is.character(kind ) || length(kind ) > 1L )
@@ -51,19 +52,34 @@ RNGkind <- function(kind = NULL, normal.kind = NULL)
5152 domain = NA )
5253 if (normal.kind == length(n.kinds ) - 1L ) normal.kind <- - 1L
5354 }
54- r <- 1L + .Internal(RNGkind(i.knd , normal.kind ))
55- r <- c(kinds [r [1L ]], n.kinds [r [2L ]])
56- if (do.set || ! is.null(normal.kind )) invisible (r ) else r
55+
56+ if (! is.null(sample.kind )) {
57+ if (! is.character(sample.kind ) || length(sample.kind ) != 1L )
58+ stop(" 'sample.kind' must be a character string of length 1" )
59+ sample.kind <- pmatch(sample.kind , s.kinds ) - 1L
60+ if (is.na(sample.kind ))
61+ stop(gettextf(" '%s' is not a valid choice" , sample.kind ),
62+ domain = NA )
63+ if (sample.kind == 0L )
64+ warning(" non-uniform 'Rounding' sampler used" ,
65+ domain = NA )
66+ if (sample.kind == length(s.kinds ) - 1L ) sample.kind <- - 1L
67+ }
68+ r <- 1L + .Internal(RNGkind(i.knd , normal.kind , sample.kind ))
69+ r <- c(kinds [r [1L ]], n.kinds [r [2L ]], s.kinds [r [3L ]])
70+ if (do.set || ! is.null(normal.kind ) || ! is.null(sample.kind ))
71+ invisible (r ) else r
5772}
5873
59- set.seed <- function (seed , kind = NULL , normal.kind = NULL )
74+ set.seed <- function (seed , kind = NULL , normal.kind = NULL , sample.kind = NULL )
6075{
6176 kinds <- c(" Wichmann-Hill" , " Marsaglia-Multicarry" , " Super-Duper" ,
6277 " Mersenne-Twister" , " Knuth-TAOCP" , " user-supplied" ,
6378 " Knuth-TAOCP-2002" , " L'Ecuyer-CMRG" , " default" )
6479 n.kinds <- c(" Buggy Kinderman-Ramage" , " Ahrens-Dieter" , " Box-Muller" ,
6580 " user-supplied" , " Inversion" , " Kinderman-Ramage" ,
6681 " default" )
82+ s.kinds <- c(" Rounding" , " Rejection" , " default" )
6783 if (length(kind ) ) {
6884 if (! is.character(kind ) || length(kind ) > 1L )
6985 stop(" 'kind' must be a character string of length 1 (RNG to be used)." )
@@ -85,6 +101,18 @@ set.seed <- function(seed, kind = NULL, normal.kind = NULL)
85101 domain = NA )
86102 if (normal.kind == length(n.kinds ) - 1L ) normal.kind <- - 1L
87103 }
104+ if (! is.null(sample.kind )) {
105+ if (! is.character(sample.kind ) || length(sample.kind ) != 1L )
106+ stop(" 'sample.kind' must be a character string of length 1" )
107+ sample.kind <- pmatch(sample.kind , s.kinds ) - 1L
108+ if (is.na(sample.kind ))
109+ stop(gettextf(" '%s' is not a valid choice" , sample.kind ),
110+ domain = NA )
111+ if (sample.kind == 0L )
112+ warning(" non-uniform 'Rounding' sampler used" ,
113+ domain = NA )
114+ if (sample.kind == length(s.kinds ) - 1L ) sample.kind <- - 1L
115+ }
88116 .Internal(set.seed(seed , i.knd , normal.kind ))
89117}
90118
@@ -96,9 +124,11 @@ RNGversion <- function(vstr)
96124 if (length(vnum ) < 2L )
97125 stop(" malformed version string" )
98126 if (vnum [1L ] == 0 && vnum [2L ] < 99 )
99- RNGkind(" Wichmann-Hill" , " Buggy Kinderman-Ramage" )
127+ RNGkind(" Wichmann-Hill" , " Buggy Kinderman-Ramage" , " Rounding " )
100128 else if (vnum [1L ] == 0 || vnum [1L ] == 1 && vnum [2L ] < = 6 )
101- RNGkind(" Marsaglia-Multicarry" , " Buggy Kinderman-Ramage" )
129+ RNGkind(" Marsaglia-Multicarry" , " Buggy Kinderman-Ramage" , " Rounding" )
130+ else if (vnum [1L ] < = 2 || vnum [1L ] == 3 && vnum [2L ] < = 5 )
131+ RNGkind(" Mersenne-Twister" , " Inversion" , " Rounding" )
102132 else
103- RNGkind(" Mersenne-Twister" , " Inversion" )
133+ RNGkind(" Mersenne-Twister" , " Inversion" , " Rejection " )
104134}
0 commit comments