SCM

SCM Repository

[rmetrics] Annotation of /pkg/randtoolbox/R/runifInterface.R
ViewVC logotype

Annotation of /pkg/randtoolbox/R/runifInterface.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4534 - (view) (download)

1 : dutangc 4229 ##
2 :     # @file runifInterface.R
3 :     # @brief R file for runif interface
4 :     #
5 :     # @author Petr Savicky
6 :     #
7 :     #
8 :     # Copyright (C) 2009, Petr Savicky, Academy of Sciences of the Czech Republic.
9 :     # All rights reserved.
10 :     #
11 :     # The new BSD License is applied to this software.
12 :     # Copyright (c) 2009 Petr Savicky.
13 :     # All rights reserved.
14 :     #
15 :     # Redistribution and use in source and binary forms, with or without
16 :     # modification, are permitted provided that the following conditions are
17 :     # met:
18 :     #
19 :     # - Redistributions of source code must retain the above copyright
20 :     # notice, this list of conditions and the following disclaimer.
21 :     # - Redistributions in binary form must reproduce the above
22 :     # copyright notice, this list of conditions and the following
23 :     # disclaimer in the documentation and/or other materials provided
24 :     # with the distribution.
25 :     # - Neither the name of the Academy of Sciences of the Czech Republic
26 :     # nor the names of its contributors may be used to endorse or promote
27 :     # products derived from this software without specific prior written
28 :     # permission.
29 :     #
30 :     # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 :     # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 :     # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 :     # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 :     # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 :     # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 :     # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 :     # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 :     # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 :     # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 :     # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 :     #
42 :     #
43 :     #############################################################################
44 :     ### runif interface
45 :     ###
46 :     ### R functions
47 :     ###
48 : dutangc 3975
49 : dutangc 4229
50 : savicky 4534 set.generator <- function(name=c("congruRand", "WELL", "default"), parameters=NULL, seed=NULL, ...,
51 : savicky 3973 only.description=FALSE)
52 : savicky 3954 {
53 : savicky 3966 name <- match.arg(name)
54 :     dots <- list(...)
55 : savicky 4534 if (name == "congruRand")
56 :     {
57 :     if (is.null(parameters))
58 : savicky 3966 parameters <- c(mod=dots$mod, mult=dots$mult, incr=dots$incr)
59 : savicky 4534 if (length(parameters) == 0)
60 : savicky 4370 parameters <- c(mod="2147483647", mult="16807", incr="0")
61 : savicky 4534 if (!identical(names(parameters), c("mod", "mult", "incr")))
62 :     {
63 : savicky 3966 param.names <- paste(names(parameters),collapse=" ")
64 : savicky 3973 stop("parameter list \"", param.names, "\" is not correct for congruRand")
65 : savicky 3966 }
66 : savicky 4534 if (is.null(seed))
67 : savicky 3973 seed <- floor(as.double(parameters["mod"]) * runif(1))
68 : savicky 4534 if (is.numeric(parameters))
69 : savicky 4370 parameters <- formatC(parameters, format="f", digits=0)
70 : savicky 4534 if (is.numeric(seed))
71 : savicky 4370 seed <- formatC(seed, format="f", digits=0)
72 : savicky 3966 state <- c(seed=seed)
73 :     description <- list(name=name, parameters=parameters, state=state)
74 : savicky 4534 } else if (name == "WELL")
75 :     {
76 :     if (is.null(parameters))
77 :     parameters <- c(order=dots$order, version=dots$version)
78 :     if (length(parameters) == 0)
79 :     parameters <- c(order=19937, version=1)
80 :     if (!identical(names(parameters), c("order", "version")))
81 :     {
82 :     param.names <- paste(names(parameters),collapse=" ")
83 :     stop("parameter list \"", param.names, "\" is not correct for WELL")
84 : savicky 3973 }
85 : savicky 4534 if (is.null(seed))
86 :     seed <- floor(2^31 * runif(1))
87 :     size <- ceiling(parameters["order"]/32)
88 :     state <- .C("initMT2002",
89 :     as.integer(seed),
90 :     as.integer(size),
91 :     integer(size),
92 :     PACKAGE="rngWELL")[[3]]
93 :     description <- list(name=name, parameters=parameters, state=state)
94 :     } else if (name == "default")
95 :     {
96 : savicky 3954 RNGkind("default")
97 : savicky 4534 if (!is.null(seed))
98 : savicky 3954 set.seed(seed)
99 : savicky 4534 return(invisible(NULL))
100 :     } else
101 : savicky 3966 stop("unsupported generator: ", name)
102 : savicky 4534 if (only.description)
103 :     return(description)
104 :     put.description(description)
105 : savicky 3954 invisible(NULL)
106 :     }
107 :    
108 : savicky 4373 put.description <- function(description)
109 : savicky 3954 {
110 : savicky 3966 name <- description$name
111 :     parameters <- description$parameters
112 :     state <- description$state
113 : savicky 4534 if (name == "congruRand")
114 :     {
115 : savicky 4375 aux <- .C("put_state_congru",
116 : savicky 4370 parameters,
117 :     state,
118 : savicky 3973 err = integer(1),
119 :     PACKAGE="randtoolbox")
120 : savicky 4375 if (aux$err != 0)
121 : savicky 3973 stop("check congruRand error: ", aux$err)
122 : savicky 4534 if (RNGkind()[1] != "user-supplied")
123 :     {
124 : savicky 4392 .C("set_noop", PACKAGE="randtoolbox")
125 : savicky 4375 RNGkind("user-supplied")
126 :     aux <- .C("put_state_congru",
127 :     parameters,
128 :     state,
129 :     err = integer(1),
130 :     PACKAGE="randtoolbox")
131 :     if (aux$err != 0)
132 :     stop("check congruRand error: ", aux$err)
133 :     }
134 : savicky 4534 } else if (name == "WELL")
135 :     {
136 :     .C("set_noop", PACKAGE="randtoolbox")
137 :     RNGkind("user-supplied")
138 :     .C("putRngWELL",
139 :     as.integer(parameters["order"]),
140 :     as.integer(parameters["version"]),
141 :     as.integer(0),
142 :     as.integer(state),
143 :     PACKAGE="rngWELL")
144 :     } else
145 : savicky 3966 stop("unsupported generator: ", name)
146 : savicky 3954 invisible(NULL)
147 :     }
148 :    
149 : savicky 4373 get.description <- function()
150 : savicky 3954 {
151 : savicky 4534 if (RNGkind(NULL)[1] != "user-supplied")
152 :     stop("For R base generators, use .Random.seed, not get.description()")
153 : savicky 3954 generator <- .C("current_generator",
154 :     integer(1),
155 :     PACKAGE="randtoolbox")[[1]]
156 : savicky 4534 if (generator == 1)
157 :     {
158 : savicky 3966 name <- "congruRand"
159 : savicky 4370 outspace <- "18446744073709551616" # 2^64
160 : savicky 3954 aux <- .C("get_state_congru",
161 : savicky 4370 parameters=rep(outspace, times=3),
162 :     seed=outspace,
163 : savicky 3954 PACKAGE="randtoolbox")
164 : savicky 4370 parameters <- aux$parameters
165 :     seed <- aux$seed
166 : savicky 3966 state <- c(seed=aux$seed)
167 : savicky 4370 if(parameters[1] == "4294967296" && parameters[2] == "1664525" && parameters[3] == "1013904223")
168 :     literature <- "Knuth - Lewis"
169 :     else if(parameters[1] == "281474976710656" && parameters[2] == "31167285" && parameters[3] == "1")
170 :     literature <- "Lavaux - Jenssens"
171 :     else if(parameters[1] == "18446744073709551616" && parameters[2] == "636412233846793005" && parameters[3] == "1")
172 :     literature <- "Haynes"
173 :     else if(parameters[1] == "4294967296" && parameters[2] == "69069" && parameters[3] == "0")
174 :     literature <- "Marsaglia"
175 :     else if(parameters[1] == "4294967295" && parameters[2] == "16807" && parameters[3] == "0")
176 :     literature <- "Park - Miller"
177 :     else
178 :     literature <- "Unknown"
179 : savicky 4534 } else if (generator == 2)
180 :     {
181 :     name <- "WELL"
182 :     tmp <- .C("getRngWELL",
183 :     order = integer(1),
184 :     version = integer(1),
185 :     temp = integer(1),
186 :     state = integer(2000),
187 :     PACKAGE="rngWELL")
188 :     parameters <- c(order=tmp$order, version=tmp$version)
189 :     size <- ceiling(parameters["order"]/32)
190 :     state <- tmp$state[1:size]
191 :     literature <- "Panneton - L'Ecuyer - Matsumoto"
192 :     } else
193 : savicky 3954 stop("internal error of randtoolbox")
194 : dutangc 3975 list(name=name, authors=literature, parameters=parameters, state=state)
195 : savicky 3954 }
196 :    

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge