Skip to content

Commit 246d370

Browse files
committed
Also expose uniform generator
1 parent 628c7b3 commit 246d370

File tree

7 files changed

+43
-1
lines changed

7 files changed

+43
-1
lines changed

ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2025-01-03 Dirk Eddelbuettel <[email protected]>
2+
3+
* inst/include/Ziggurat.h (runi): Expose interally used uniform RNG
4+
* src/ziggurat.cpp (zruni): Provide R-accessible wrapper for runi()
5+
* man/ziggurat.Rd: Document zrexp() and zruni()
6+
* src/RcppExports.cpp: Regenerated
7+
* R/RcppExports.R: Idem
8+
19
2025-01-02 Dirk Eddelbuettel <[email protected]>
210

311
* inst/include/Ziggurat.h (kiss): Use an inline function avoiding a

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Description: The Ziggurat generator for normally distributed random
1010
<doi:10.18637/jss.v005.i08>) has been improved upon a few times
1111
starting with Leong et al (2005, <doi:10.18637/jss.v012.i07>).
1212
This package provides an aggregation in order to compare different
13-
implementations in order to provide an 'faster but good enough'
13+
implementations in order to provide a 'faster but good enough'
1414
alternative for use with R and C++ code.
1515
License: GPL (>= 2)
1616
Depends: R (>= 3.0.0)

R/RcppExports.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ zrexp <- function(n) {
4545
.Call(`_RcppZiggurat_zrexp`, n)
4646
}
4747

48+
zruni <- function(n) {
49+
.Call(`_RcppZiggurat_zruni`, n)
50+
}
51+
4852
zrnormVec <- function(x) {
4953
.Call(`_RcppZiggurat_zrnormVec`, x)
5054
}

inst/include/Ziggurat.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ namespace Ziggurat {
9696
inline double rexp() {
9797
return (jz = kiss(), iz = jz & 255, ( jz < ke[iz] ) ? jz * we[iz] : efix());
9898
}
99+
inline double runi() {
100+
return ( 0.5 + ( signed ) KISS * 0.2328306e-09 );
101+
}
99102
std::vector<uint32_t> getPars() {
100103
//C++11: std::vector<uint32_t> pars{ jsr, z, w, jcong };
101104
std::vector<uint32_t> pars;

man/ziggurat.Rd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
\name{zrnorm}
22
\alias{zrnorm}
33
\alias{zrexp}
4+
\alias{zruni}
45
\alias{zrnormLZLLV}
56
\alias{zrnormMT}
67
\alias{zrnormV1}
@@ -38,6 +39,7 @@
3839
\usage{
3940
zrnorm(n)
4041
zrexp(n)
42+
zruni(n)
4143
zrnormLZLLV(n)
4244
zrnormMT(n)
4345
zrnormV1(n)
@@ -76,6 +78,10 @@
7678
actually not sufficient to capture the state, so \code{zgetpars}
7779
\code{zsetpars} provide this functionality with a vector of size four.
7880
Use these functions with caution.
81+
82+
Following the original paper, \code{rexp} also provides a vector of random
83+
draws from the exponential distribution. In addition, \code{runi} offers random
84+
draws from the uniform distribution (which is used internally).
7985

8086
}
8187
\details{

src/RcppExports.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ BEGIN_RCPP
128128
return rcpp_result_gen;
129129
END_RCPP
130130
}
131+
// zruni
132+
Rcpp::NumericVector zruni(int n);
133+
RcppExport SEXP _RcppZiggurat_zruni(SEXP nSEXP) {
134+
BEGIN_RCPP
135+
Rcpp::RObject rcpp_result_gen;
136+
Rcpp::RNGScope rcpp_rngScope_gen;
137+
Rcpp::traits::input_parameter< int >::type n(nSEXP);
138+
rcpp_result_gen = Rcpp::wrap(zruni(n));
139+
return rcpp_result_gen;
140+
END_RCPP
141+
}
131142
// zrnormVec
132143
Rcpp::NumericVector zrnormVec(Rcpp::NumericVector x);
133144
RcppExport SEXP _RcppZiggurat_zrnormVec(SEXP xSEXP) {
@@ -332,6 +343,7 @@ static const R_CallMethodDef CallEntries[] = {
332343
{"_RcppZiggurat_zgetseedV1", (DL_FUNC) &_RcppZiggurat_zgetseedV1, 0},
333344
{"_RcppZiggurat_zrnorm", (DL_FUNC) &_RcppZiggurat_zrnorm, 1},
334345
{"_RcppZiggurat_zrexp", (DL_FUNC) &_RcppZiggurat_zrexp, 1},
346+
{"_RcppZiggurat_zruni", (DL_FUNC) &_RcppZiggurat_zruni, 1},
335347
{"_RcppZiggurat_zrnormVec", (DL_FUNC) &_RcppZiggurat_zrnormVec, 1},
336348
{"_RcppZiggurat_zrnormStl", (DL_FUNC) &_RcppZiggurat_zrnormStl, 1},
337349
{"_RcppZiggurat_zsetseed", (DL_FUNC) &_RcppZiggurat_zsetseed, 1},

src/ziggurat.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ Rcpp::NumericVector zrexp(int n) {
139139
return x;
140140
}
141141

142+
// [[Rcpp::export]]
143+
Rcpp::NumericVector zruni(int n) {
144+
Rcpp::NumericVector x(n);
145+
for (int i=0; i<n; i++) {
146+
x[i] = zigg.runi();
147+
}
148+
return x;
149+
}
150+
142151
// [[Rcpp::export]]
143152
Rcpp::NumericVector zrnormVec(Rcpp::NumericVector x) {
144153
int n = x.size();

0 commit comments

Comments
 (0)