Discussion:
[Rcpp-devel] Setting seed using Rcpp
Hmamouche Youssef
2018-02-07 15:20:16 UTC
Permalink
Hi,

I get a note from R CMD check for an Rcpp package:

Found ‘_srand’, possibly from ‘srand’

Found no calls to: ‘R_registerRoutines’, ‘R_useDynamicSymbols’


Is there some an equivalent function for srand from Rcpp, which i can use
it inside c++ code?

Best regards,
Youssef
Serguei Sokol
2018-02-07 15:36:11 UTC
Permalink
Hi,
Found ‘_srand’, possibly from ‘srand’
Found no calls to: ‘R_registerRoutines’, ‘R_useDynamicSymbols’
Is there some an  equivalent function for srand from Rcpp, which i can use it inside c++ code?
If you try "rcpp random number generation" in a search engine,
the first line will point to

http://gallery.rcpp.org/articles/random-number-generation/

which probably has your answer.

Best,
Serguei.
Dirk Eddelbuettel
2018-02-07 15:36:28 UTC
Permalink
On 7 February 2018 at 16:20, Hmamouche Youssef wrote:
| Hi,
|
| I get a note from R CMD check for an Rcpp package:
|
| Found ‘_srand’, possibly from ‘srand’
|
| Found no calls to: ‘R_registerRoutines’, ‘R_useDynamicSymbols’
|
|
| Is there some an equivalent function for srand from Rcpp, which i can use
| it inside c++ code?

In short, no.

Slightly longer: All Rcpp code is set up to be used _from R_ so by design you
are always in R before you call code containing Rcpp. So just call the R
function set.seed(...) as we work hard to make sure the state is properly set
and passed before and after.

Even longer: srand() is always wrong and bad (long literature on that ...)
and has nothing to do with R's RNG. You are entirely free to define and
implement your own RNG (see my RcppZiggurrat package revisiting Marsaglia's
Ziggurat RNG) or call others (C++11 has some) or ... But you probably want
to use R's RNGs, and for that you have to set them from R (or use
Rcpp::Function()). Also see the docs on the R Math Library (Section 6.16 of
Writing R Extensions) which gives you special functions and RNGs and even
set_seed() but that is for use _apart from R_ which is NOT what we do here.

Dirk
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
Hao Ye
2018-02-07 15:44:03 UTC
Permalink
Hi Youssef,

Rcpp has sugar for the R random number generators. This is nice if you want
to let your users set the random seed in R code outside of the package for
reproducibility: http://gallery.rcpp.org/articles/random-number-generation/

Alternatively, if you enable C++11, there are some random number generators
in std: https://www.guyrutenberg.com/2014/05/03/c-mt19937-example/

Best,
--
Hao Ye

On Wed, Feb 7, 2018 at 10:20 AM, Hmamouche Youssef <
Post by Hmamouche Youssef
Hi,
Found ‘_srand’, possibly from ‘srand’
Found no calls to: ‘R_registerRoutines’, ‘R_useDynamicSymbols’
Is there some an equivalent function for srand from Rcpp, which i can
use it inside c++ code?
Best regards,
Youssef
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Hmamouche Youssef
2018-02-07 16:20:08 UTC
Permalink
Thank you all for these answers.

If you try "rcpp random number generation" in a search engine,
the first line will point to

http://gallery.rcpp.org/articles/random-number-generation/

which probably has your answer.

Best,
Serguei

Yes of course i tried http://gallery.rcpp.org/articl
es/random-number-generation/, which helps to replace the rand function, and
effectively, i found the set.seed () (from R), just i didn't know how t use
it in C++.
-----------------------------------------------------------------
-----------------

In short, no.

Slightly longer: All Rcpp code is set up to be used _from R_ so by design
you
are always in R before you call code containing Rcpp. So just call the R
function set.seed(...) as we work hard to make sure the state is properly
set
and passed before and after.

Even longer: srand() is always wrong and bad (long literature on that ...)
and has nothing to do with R's RNG. You are entirely free to define and
implement your own RNG (see my RcppZiggurrat package revisiting Marsaglia's
Ziggurat RNG) or call others (C++11 has some) or ... But you probably want
to use R's RNGs, and for that you have to set them from R (or use
Rcpp::Function()). Also see the docs on the R Math Library (Section 6.16 of
Writing R Extensions) which gives you special functions and RNGs and even
set_seed() but that is for use _apart from R_ which is NOT what we do here.

Thx, I will look at RcppZiggurrat package, and also try other C++ functions.
-----------------------------------------------------------------
-----------------

Hi Youssef,

Rcpp has sugar for the R random number generators. This is nice if you want
to let your users set the random seed in R code outside of the package for
reproducibility: http://gallery.rcpp.org/articles/random-number-generation/

Alternatively, if you enable C++11, there are some random number generators
in std: https://www.guyrutenberg.com/2014/05/03/c-mt19937-example/

Best,
--
Hao Ye

Thx, in my case, i have it initialize the number generator, so i will try
check the proposed link.
-----------------------------------------------------------------
-----------------


Best regards,
Youssef
Post by Hao Ye
Hi Youssef,
Rcpp has sugar for the R random number generators. This is nice if you
want to let your users set the random seed in R code outside of the package
for reproducibility: http://gallery.rcpp.org/articles/
random-number-generation/
Alternatively, if you enable C++11, there are some random number
generators in std: https://www.guyrutenberg.com/2014/05/03/c-mt19937-
example/
Best,
--
Hao Ye
On Wed, Feb 7, 2018 at 10:20 AM, Hmamouche Youssef <
Post by Hmamouche Youssef
Hi,
Found ‘_srand’, possibly from ‘srand’
Found no calls to: ‘R_registerRoutines’, ‘R_useDynamicSymbols’
Is there some an equivalent function for srand from Rcpp, which i can
use it inside c++ code?
Best regards,
Youssef
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Loading...