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 3966 - (view) (download)

1 : savicky 3966 set.generator <- function(name=c("congruRand", "default"), parameters=NULL, seed=NULL, ...)
2 : savicky 3954 {
3 : savicky 3966 name <- match.arg(name)
4 :     dots <- list(...)
5 :     if (name == "congruRand") {
6 :     if (is.null(parameters)) {
7 :     parameters <- c(mod=dots$mod, mult=dots$mult, incr=dots$incr)
8 : savicky 3954 }
9 : savicky 3966 if (length(parameters) == 0) {
10 :     parameters <- c(mod=2147483647, mult=16807, incr=0)
11 :     }
12 :     if (!identical(names(parameters), c("mod", "mult", "incr"))) {
13 :     param.names <- paste(names(parameters),collapse=" ")
14 :     stop("parameters \"", param.names, "\" are not correct for congruRand")
15 :     }
16 : savicky 3954 if (is.null(seed)) {
17 : savicky 3966 seed <- floor(2^32 * runif(1))
18 : savicky 3954 }
19 : savicky 3966 state <- c(seed=seed)
20 :     description <- list(name=name, parameters=parameters, state=state)
21 :     put.state(description)
22 :     } else if (name == "default") {
23 : savicky 3954 RNGkind("default")
24 :     if (!is.null(seed)) {
25 :     set.seed(seed)
26 :     }
27 :     } else {
28 : savicky 3966 stop("unsupported generator: ", name)
29 : savicky 3954 }
30 :     invisible(NULL)
31 :     }
32 :    
33 : savicky 3966 put.state <- function(description)
34 : savicky 3954 {
35 : savicky 3966 name <- description$name
36 :     parameters <- description$parameters
37 :     state <- description$state
38 :     if (name == "congruRand") {
39 : savicky 3954 .C("set_generator",
40 :     as.integer(1),
41 :     PACKAGE="randtoolbox")
42 : savicky 3958 RNGkind("user-supplied")
43 : savicky 3954 .C("put_state_congru",
44 : savicky 3966 as.double(parameters["mod"]),
45 :     as.double(parameters["mult"]),
46 :     as.double(parameters["incr"]),
47 :     as.double(state["seed"]),
48 : savicky 3954 PACKAGE="randtoolbox")
49 :     } else {
50 : savicky 3966 stop("unsupported generator: ", name)
51 : savicky 3954 }
52 :     invisible(NULL)
53 :     }
54 :    
55 :     get.state <- function()
56 :     {
57 :     if (RNGkind(NULL)[1] != "user-supplied") {
58 : savicky 3958 stop("For R base generators, use .Random.seed, not get.state()")
59 : savicky 3954 }
60 :     generator <- .C("current_generator",
61 :     integer(1),
62 :     PACKAGE="randtoolbox")[[1]]
63 :     if (generator == 1) {
64 : savicky 3966 name <- "congruRand"
65 : savicky 3954 aux <- .C("get_state_congru",
66 :     mod=double(1),
67 :     mult=double(1),
68 :     incr=double(1),
69 :     seed=double(1),
70 :     PACKAGE="randtoolbox")
71 : savicky 3966 parameters <- c(mod=aux$mod, mult=aux$mult, incr=aux$incr)
72 :     state <- c(seed=aux$seed)
73 : savicky 3954 } else {
74 :     stop("internal error of randtoolbox")
75 :     }
76 : savicky 3966 list(name=name, parameters=parameters, state=state)
77 : savicky 3954 }
78 :    

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