{"action":"created","issue":{"url":"https://api.github.com/repos/RcppCore/Rcpp/issues/268","labels_url":"https://api.github.com/repos/RcppCore/Rcpp/issues/268/labels{/name}","comments_url":"https://api.github.com/repos/RcppCore/Rcpp/issues/268/comments","events_url":"https://api.github.com/repos/RcppCore/Rcpp/issues/268/events","html_url":"https://github.com/RcppCore/Rcpp/issues/268","id":59082895,"number":268,"title":"Problem with automatic RNG scoping","user":{"login":"romainfrancois","id":2625526,"avatar_url":"https://avatars.githubusercontent.com/u/2625526?v=3","gravatar_id":"","url":"https://api.github.com/users/romainfrancois","html_url":"https://github.com/romainfrancois","followers_url":"https://api.github.com/users/romainfrancois/followers","following_url":"https://api.github.com/users/romainfrancois/following{/other_user}","gists_url":"https://api.github.com/users/romainfrancois/gists{/gist_id}","starred_url":"https://api.github.com/users/romainfrancois/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/romainfrancois/subscriptions","organizations_url":"https://api.github.com/users/romainfrancois/orgs","repos_url":"https://api.github.com/users/romainfrancois/repos","events_url":"https://api.github.com/users/romainfrancois/events{/privacy}","received_events_url":"https://api.github.com/users/romainfrancois/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-02-26T14:49:38Z","updated_at":"2015-02-26T15:15:14Z","closed_at":null,"body":"Consider: \r\n\r\n```cpp\r\n#include <Rcpp.h>\r\nusing namespace Rcpp ;\r\n\r\n// [[Rcpp::export]]\r\nList test_rnorm(){\r\n    Function r_rnorm(\"rnorm\") ;\r\n    \r\n    NumericVector x = rnorm(2) ;\r\n    NumericVector y = r_rnorm(2) ;\r\n    \r\n    return List::create(x, y );\r\n    \r\n}\r\n\r\n/*** R \r\n    test_rnorm()\r\n*/\r\n```\r\n\r\nI get: \r\n\r\n```\r\n> test_rnorm()\r\n[[1]]\r\n[1] -0.1906103  0.5690450\r\n\r\n[[2]]\r\n[1] -0.1906103  0.5690450\r\n```\r\n\r\nObviously because the seed is not `put` between the `C++` call to `Rcpp::rnorm` and the `rnorm` function from R. \r\n\r\nOf course this is trivial in this example because I'm just calling `rnorm`, but this could have been calling some other R function that internally does the `get` and `put` dance. \r\n\r\nGoing further, if we consider this one now: \r\n\r\n```cpp\r\n#include <Rcpp.h>\r\nusing namespace Rcpp ;\r\n\r\n// [[Rcpp::export]]\r\nList test_rnorm( int ncpp, int nr ){\r\n    Function r_rnorm(\"rnorm\") ;\r\n    \r\n    NumericVector x = rnorm(ncpp) ;\r\n    NumericVector y = r_rnorm(nr) ;\r\n    \r\n    return List::create(x, y );\r\n    \r\n}\r\n\r\n/*** R \r\n    test_rnorm(4, 2); rnorm(2)\r\n*/\r\n```\r\n\r\nwhich gives: \r\n\r\n```\r\n> test_rnorm(4, 2)\r\n[[1]]\r\n[1]  0.9418597  2.0877568  0.9308808 -0.2713577\r\n\r\n[[2]]\r\n[1] 0.9418597 2.0877568\r\n\r\n\r\n> rnorm(2)\r\n[1]  0.9308808 -0.2713577\r\n```\r\n\r\nSo I think implicit automatic rng scoping is wrong. Not sure about a workaround though. I guess reverting to explicit uses of `RNGScope` can't be done, etc ...\r\n"},"comment":{"url":"https://api.github.com/repos/RcppCore/Rcpp/issues/comments/76194970","html_url":"https://github.com/RcppCore/Rcpp/issues/268#issuecomment-76194970","issue_url":"https://api.github.com/repos/RcppCore/Rcpp/issues/268","id":76194970,"user":{"login":"eddelbuettel","id":673121,"avatar_url":"https://avatars.githubusercontent.com/u/673121?v=3","gravatar_id":"","url":"https://api.github.com/users/eddelbuettel","html_url":"https://github.com/eddelbuettel","followers_url":"https://api.github.com/users/eddelbuettel/followers","following_url":"https://api.github.com/users/eddelbuettel/following{/other_user}","gists_url":"https://api.github.com/users/eddelbuettel/gists{/gist_id}","starred_url":"https://api.github.com/users/eddelbuettel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eddelbuettel/subscriptions","organizations_url":"https://api.github.com/users/eddelbuettel/orgs","repos_url":"https://api.github.com/users/eddelbuettel/repos","events_url":"https://api.github.com/users/eddelbuettel/events{/privacy}","received_events_url":"https://api.github.com/users/eddelbuettel/received_events","type":"User","site_admin":false},"created_at":"2015-02-26T15:15:14Z","updated_at":"2015-02-26T15:15:14Z","body":"Nice example.\r\n\r\nBut my first reaction is what I would say to someone who claims `unlink()` is dangerous...\r\n\r\nTo me, this seems mostly like a documentation issue as it is indeed hard to safeguard against bad use.  We give three different ways to get to the RNGs from the C++ level; if folks then insist on bypassing the carefully constructed framework they will have to live with the consequences of their actions.    That said, if we had a way to warn more directly / make it more difficult I would be in favour too.  "}}