Git Product home page Git Product logo

Comments (27)

eddelbuettel avatar eddelbuettel commented on June 12, 2024

Here is a simpler version, courtesy of the rest of Rcpp around it:

R> cppFunction('void setArmaRNGseed(const unsigned int seed) { srand(seed); }', 
+              depends="RcppArmadillo")
R> setArmaRNGseed(1234)
R> 

We can obviously pack more error checking around it...

This is of no particular interest to me, though, as Armadillo uses library RNGs (and has every right to do so -- it can only assume so and so much) yet we have better RNGs in R, which are also faster. I have timings for that on the Rcpp Gallery.

So if you really want to use this RNG (for your reproducibility, say) go ahead but I am not sure I'd want it more prominently in RcppArmadillo at this point. We could write a short Rcpp Gallery piece though.

from rcpparmadillo.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

Sure, it is simpler with Rcpp. In igraph I wanted a "pure" solution to avoid an Rcpp dependency just because of this. But you are right that the Rcpp solution is better in RcppArmadillo.

It's not that I want to use this RNG, I don't. But some packages like ngspatial are using it, and then if I want to make code that uses ngspatial reproducible, then I need to be able to set the seed.

Ideally, ngspatial should not use this RNG, or at least should provide a way to set the random seed.

So if you don't want to put it in RcppArmadillo, that's your decision, I'll just keep it in igraph, then. (Totally out of place, but I don't want to create another package just because of this.)

from rcpparmadillo.

eddelbuettel avatar eddelbuettel commented on June 12, 2024

Hm. Maybe we need talk to the ngspatial folks and have them use the R RNGs?

Otherwise, what is the link between igraph and ngspatial?

A seed setter / getter pair is simple enough particularly for these simple RNGs. We could add them, but as RcppArmadillo is an Rcpp package we would not do them the way you did. Use Rcpp Attributes if you still feel this needs to be added at my end. Else, if you sit on top of ngspatial, you are by construction also on top of RcppArmadillo and have the same interfaces at your disposal. I'd use them -- shorter / more concise code is easier to get right.

from rcpparmadillo.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

There is no link between igraph and ngspatial, really. They are both used in a project I am in, and I cannot modify ngspatial, but I can easily modify igraph. Or I could just create an srand package that does this.

I think there are two logical places for these functions. The first is RcppArmadillo, because that package provides the RNGs after all, even if their use is discouraged. (Although some example RcppArmadillo code uses them, I guess that's why people started using them as well.)

The second is the ngspatial package (and any other package that uses the armadillo RNGs).

I can talk to the ngspatial people as well, the reason I chose RcppArmadillo was that, then it only needs to be done once, and we are good for all packages.

I get it that you would want to add this the Rcpp way, if at all. Btw. I am not very familiar with Rcpp, but don't you need to have a C compiler at run time to set the seed, if the functions are implemented your way?

from rcpparmadillo.

eddelbuettel avatar eddelbuettel commented on June 12, 2024

Unless you are on the retard OS, you always need a compiler to install the packages.

We live of packaged tarballs, or source repos. [ Well, ok, I also maintain r-cran-rcpparmadillo but it lags behind unless you run cutting edge Debian unstable / testing if which case you're a grown-up anyway and don't need hand-holding. ]

from rcpparmadillo.

eddelbuettel avatar eddelbuettel commented on June 12, 2024

Also, do you have a reference to your stated "some example RcppArmadillo code use[s] them" ? I am not aware of having written example using Arma's RNGs but maybe others have.

from rcpparmadillo.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

Well, OSX and Windows both use binary R packages by default, so no compiler might be installed. You would think that if a package is using Rcpp, then it requires a compiler at run time, but ngspatial does not need one AFAIK.

While I was searching for a way to set RcppArmadillo's random seed, I found a couple, e.g. these: http://dirk.eddelbuettel.com/blog/2012/09/02/
http://dirk.eddelbuettel.com/papers/rcpparmadillo_rfinance_may2013.pdf
and others, search for RcppArmadillo randu or randn

randu and randn are calling armadillo's RNG, right?

from rcpparmadillo.

eddelbuettel avatar eddelbuettel commented on June 12, 2024

Problem is that even on OS X, every now and then either the toolchain changes, or an ABI/API change happens so we're actually telling people pretty regularly to (re-)install Rcpp from source on OS X. Then again, I am a bystander here as I don't have any OS X systems.

The two posts do indeed call Arma RNGs but consider a) that Sep 2009 blog post does it in the context of a larger comparison, and it does turn to be slower as I said earlier and b) is largely our published CSDA paper which also covers some base Arma so sure, it is in there.

It is good to have these basic generators as fallback, but in the work we facilitate here between R and Armadillo there are superior alternatives to be had. If you need seed setters / getters, local helper functions would be one way. I won;t promise I;ll add this at this end as it really runs counter to what RcppArmadillo is primarily about.

from rcpparmadillo.

eddelbuettel avatar eddelbuettel commented on June 12, 2024

Hm, I added a very basic

// [[Rcpp::export]]
void armadillo_set_seed_random() {
    arma::arma_rng::set_seed_random();              // set the seed to a random value
} 

// [[Rcpp::export]]
void armadillo_set_seed(unsigned int val) {
    Rcpp::Rcout << "Setting value " << val << std::endl;
    arma::arma_rng::set_seed(val);                  // set the seed to given value
} 

but alas, no beans:

R> library(RcppArmadillo)
R> RcppArmadillo:::armadillo_set_seed(42)
Setting value 42
R> evalCpp("arma::mat(2,2).randn()", depends="RcppArmadillo")
         [,1]     [,2]
[1,]  1.80547 0.166367
[2,] -1.16046 0.798098
R> RcppArmadillo:::armadillo_set_seed(42)
Setting value 42
R> evalCpp("arma::mat(2,2).randn()", depends="RcppArmadillo")
          [,1]      [,2]
[1,] -0.927823 0.5920569
[2,] -0.169613 0.0639354
R> 

It works as expected if you do it at the Armadillo code level (NB: code indented here, it really is one line in R):

R> cppFunction('arma::mat foo(unsigned int seed) { 
                arma::arma_rng::set_seed(seed); 
                return arma::mat(2,2).randn(); }', depends="RcppArmadillo")
R> foo(42)
           [,1]      [,2]
[1,] -0.1563459  1.745620
[2,] -0.0569833 -0.709777
R> foo(42)
           [,1]      [,2]
[1,] -0.1563459  1.745620
[2,] -0.0569833 -0.709777
R> 

which pretty much neuters the idea of exporting it to RcppArmadillo.

I think I have no choice but to close this. Thoughts?

from rcpparmadillo.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

To be honest I don't understand why the first case doesn't work. But if you say it does not, then this is it, I guess.

from rcpparmadillo.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

Actually, I just tried your first method, and it seems to work fine, in fact I get exactly the same matrices as in the second method (as expected). Could you try it again?

I used the the github version of Rcpp and RcppArmadillo, added back the commented out seed functions, and run compileAttributes and R CMD INSTALL, and then your code above.

Probably does not matter, but this was in OSX. It might matter, actually, I can also try it on Linux, if you still cannot make it to work.

from rcpparmadillo.

eddelbuettel avatar eddelbuettel commented on June 12, 2024

Awesome. Me too -- if and only if I do it on the command-line.

If I do it in RStudio, as I had done here first (for build convenience) then it does not work. Ergo, RStudio has a bad side-effect of (re-)setting srand. Paging @jjallaire ...

from rcpparmadillo.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

Thanks, Dirk. I did not think it would be this much trouble, sorry about it. I can still write the docs, and submit a pull request if you want.

from rcpparmadillo.

eddelbuettel avatar eddelbuettel commented on June 12, 2024

No trouble at all. Those were two two-liners. But we need to figure why RStudio gets in the way. Paging @jjallaire ...

from rcpparmadillo.

eddelbuettel avatar eddelbuettel commented on June 12, 2024

Just to formally note that all this is now in the repo, including proper help files and all.

from rcpparmadillo.

jjallaire avatar jjallaire commented on June 12, 2024

Trying to look into this now but aren't familiar enough with RcppArmadillo
to fully follow the thread. Could you give the the basic "I did this, got
this, but expect that" summary and/or some example code for me to try it
with?

On Tue, Feb 4, 2014 at 7:15 AM, Dirk Eddelbuettel
[email protected]:

Awesome. Me too -- if and only if I do it on the command-line.

If I do it in RStudio, as I had done here first (for build convenience)
then it does not work. Ergo, RStudio has a bad side-effect of (re-)setting
srand. Pageing @jjallaire https://github.com/jjallaire ...

Reply to this email directly or view it on GitHubhttps://github.com//issues/11#issuecomment-34053785
.

from rcpparmadillo.

eddelbuettel avatar eddelbuettel commented on June 12, 2024

Scroll back a little in the thread. When using evalCpp to get a 2x2 matrix, setting the rng has no effect iff done in rstudio.
Works on cmdline / termunal r session.

from rcpparmadillo.

eddelbuettel avatar eddelbuettel commented on June 12, 2024

Ie look at this earlier comment

from rcpparmadillo.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

I guess that issue is that Armadillo is using the underlying libc random number generator, and rstudio also touches the seed of this rng, or just generates random numbers with it, ie. it calls rand() or srand().

Unfortunately there is no way to save the seed of these RNGs AFAIK, so if RStudio really needs random numbers, then I am not sure what it could do.

from rcpparmadillo.

eddelbuettel avatar eddelbuettel commented on June 12, 2024

Not use the system RNG :) Ie use R's RNG. Or work outside of RStudio.

from rcpparmadillo.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

I mean the solution for RStudio. If they are calling srand/rand before actually staring R, then they cannot use R's RNGs. Or the call might be in a library they use, which is hard to change to use R's RNGs.

from rcpparmadillo.

eddelbuettel avatar eddelbuettel commented on June 12, 2024

I think RStudio does the right thing: use the system library RNG, because that leaves R alone. You have a particular in that you don't want to use R's RNG as it is an Armadillo implementation, and that now conflicts with your IDE preference. Methinks you may have to switch one or the other.

from rcpparmadillo.

jjallaire avatar jjallaire commented on June 12, 2024

In terms of RStudio use of random numbers, we do generate UUIDs in several places using this:

http://linux.die.net/man/3/uuid_generate_random

Would that be what's introducing this behavior? If so we could change to uuid_generate_time and perhaps that would make a difference.

In RStudio Server we also read from /dev/urandom as part of initializing some cryto libraries but I'm assuming you aren't running on server.

from rcpparmadillo.

jjallaire avatar jjallaire commented on June 12, 2024

I've changed RStudio to use uuid_generate_time. The latest daily build has this change:

http://www.rstudio.org/download/daily/

Does this result in different behavior with RcppArmadillo?

from rcpparmadillo.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

I think this is going too far. The "bug" is definitely in the R packages that use Armadillo's RNG. I don't think RStudio (or any other software that embeds R) should be changed because of this. Adding the seeding functions to RcppArmadillo is reasonable, but not any more.

I think we can consider this closed, and I'll email the developers of the R package that caused this.

from rcpparmadillo.

jjallaire avatar jjallaire commented on June 12, 2024

Okay, point taken. I'll stand down on this one :-)

from rcpparmadillo.

eddelbuettel avatar eddelbuettel commented on June 12, 2024

Good. Fixing the package in question is the first thing I would have suggested.

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.