[Rcpp-commits] r1960 - pkg/Rcpp/inst/include/Rcpp/stats/random

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Aug 10 08:15:07 CEST 2010


Author: romain
Date: 2010-08-10 08:15:06 +0200 (Tue, 10 Aug 2010)
New Revision: 1960

Added:
   pkg/Rcpp/inst/include/Rcpp/stats/random/runif.h
Modified:
   pkg/Rcpp/inst/include/Rcpp/stats/random/rnorm.h
Log:
runif( int, double, double )

Modified: pkg/Rcpp/inst/include/Rcpp/stats/random/rnorm.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/rnorm.h	2010-08-10 06:03:51 UTC (rev 1959)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/rnorm.h	2010-08-10 06:15:06 UTC (rev 1960)
@@ -29,8 +29,8 @@
 class NormGenerator : public Rcpp::Generator<seed,double> {
 public:
 	
-	NormGenerator( double mean = 0.0 , double sd = 1.0 ) : 
-		mean(mean), sd(sd) {}
+	NormGenerator( double mean_ = 0.0 , double sd_ = 1.0 ) : 
+		mean(mean_), sd(sd_) {}
 	
 	inline double operator()() const {
 		return mean + sd * ::norm_rand() ;

Added: pkg/Rcpp/inst/include/Rcpp/stats/random/runif.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/stats/random/runif.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/stats/random/runif.h	2010-08-10 06:15:06 UTC (rev 1960)
@@ -0,0 +1,63 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 4 -*-
+//
+// runif.h: Rcpp R/C++ interface class library -- 
+//
+// Copyright (C) 2010 Douglas Bates, Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef Rcpp__stats__random_runif_h
+#define Rcpp__stats__random_runif_h
+
+namespace Rcpp {
+namespace stats {
+
+template <bool seed>
+class UnifGenerator : public Rcpp::Generator<seed,double> {
+public:
+	
+	UnifGenerator( double min = 0.0, double max = 1.0) : 
+		min(min_), max(max_), diff(max_ - min_) {}
+	
+	inline double operator()() const {
+		double u;
+		do {u = unif_rand();} while (u <= 0 || u >= 1);
+		return min + diff * u;
+	}
+	
+private:
+	double min; 
+	double max ;
+	double diff ;
+} ;
+
+template <bool seed>
+Rcpp::NumericVector runif__impl( int n, double min, double max ){
+	if (!R_FINITE(min) || !R_FINITE(max) || max < min) return Rcpp::NumericVector( n, R_NaN ) ;
+	if( min == max ) return Rcpp::NumericVector( n, min ) ;
+	return Rcpp::NumericVector( n, UnifGenerator<seed>( min, max ) ) ;
+}
+inline Rcpp::NumericVector runif( int n, double min = 0.0, double max = 1.0 ){
+	return runif__impl<true>( n, min, max );
+}
+inline Rcpp::NumericVector runif_( int n, double min = 0.0, double max = 1.0 ){
+	return runif__impl<false>( n, min, max );
+}
+
+}
+}
+
+#endif



More information about the Rcpp-commits mailing list