Git Product home page Git Product logo

Comments (2)

eddelbuettel avatar eddelbuettel commented on June 12, 2024

Ok, a few things:

  1. Try formatting your posts. It makes it easier for other people to read your posts, and hence more likely they will help you. I formatted it for you.
  2. You were using the older inline approach. Rcpp Attributes is nicer. A new version is below.
  3. RcppArmadillo is used from R. R has very good and extremely well-tested statistical algorithms. I would use R's RNG. In fact, that is what we do here as I recall...
  4. For me it all works.

New file:

#include <RcppArmadillo.h>

// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
arma::mat foo(int n) {
    arma::mat f = arma::join_rows(arma::randu(n),  
                                  arma::randn(n));
    return f;
}

/*** R
set.seed(123)
foo(5)
*/

which we simple feed to sourceCpp() which will also run the code:

R> set.seed(123)

R> foo(5)
          [,1]        [,2]
[1,] 0.2875775 -0.61079930
[2,] 0.7883051  0.03777546
[3,] 0.4089769  0.95877582
[4,] 0.8830174  0.12566834
[5,] 0.9404673 -0.05538708
R> 

Best of all, the seed is linked to R as we (as I recall) use R's RNG by default. Calling again gives different numbers; resetting the seed gives (as expected) what we just got:

R> foo(5)
          [,1]       [,2]
[1,] 0.4533342 -0.1193901
[2,] 0.6775706  0.3153385
[3,] 0.5726334  0.6708789
[4,] 0.1029247  0.3320531
[5,] 0.8998250  0.9669271
R> set.seed(123)
R> foo(5)
          [,1]        [,2]
[1,] 0.2875775 -0.61079930
[2,] 0.7883051  0.03777546
[3,] 0.4089769  0.95877582
[4,] 0.8830174  0.12566834
[5,] 0.9404673 -0.05538708
R> 

Strictly no issue here. I suspect that the cause may have been your use of cxxfunction() AND the random generator WITHOUT setting RNGState (see Writing R Extensions). The Rcpp Attributes approach does that automatically for you, and I no longer recall if we did that for inline too.

from rcpparmadillo.

faraway1nspace avatar faraway1nspace commented on June 12, 2024

Superb! Thanks for the detailed correction and broad lesson. This now works on RcppArmadillo 0.6.5.

from rcpparmadillo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.