> On Aug 4, 2016, at 12:44 AM, Ben Goodrich <
goodri...@gmail.com> wrote:
>
> On Wednesday, August 3, 2016 at 5:58:07 PM UTC-4, Bob Carpenter wrote:
> I have no idea how that interacts with R
>
> Not at all, except for memory consumed
>
> or what users want for reproduciblity within an R session
>
> I can't imagine anything other than it be reproducible, which it is currently.
It's reproducible, but you can't reset that reproducibility within
a session. Not sure exactly why you'd want to compute the same
thing twice, but presumably that's what they're asking. Same reason
R lets you call set.seed() as many times as you want.
> The OP believes it is a bug that
>
> foo_rng(seed = 42L) != foo_rng(seed = 42L)
>
> which is because the first call to foo_rng constructs the Boost PRNG with the static keyword and seed 42 and then updates the state of the PRNG after generating some random number. Thus, the second call to foo_rng skips constructing the Boost PRNG and does not reset the seed to 42.
I agree with the OP here, because it sure looks like those should
return the same thing. If the second call doesn't do anything
with the seed argument, it should alert the user by throwing an
exception or at the very least printing some kind of warning.
>
> If the Boost PRNG were not declared with the static keyword, then
>
> foo_rng() == foo_rng() # default seed in both cases is 0L
>
> because it would be constructed with seed 0 by the first call to foo_rng and destroyed and then reconstructed with seed 0 by the second call to foo_rng and redestroyed. This behavior would be atypical for R users since
>
> rnorm(n = 1) != rnorm(n = 1)
>
> and, if not recognized, would lead to disastrous results because all the realizations of foo_rng() would be the same.
I agree that would be bad news.
But presumably an R user would expect a and b to get the same value in:
> set.seed(42)
> a = rnorm(n = 1);
> set.seed(42)
> b = rnorm(n = 1);
> To avoid this, the user would have to pass a seed explicitly each time foo_rng was called, so that
>
> foo_rng(seed = 0L) != foo_rng(seed = 1L)
>
> which is also atypical for R.
>
> An alternative would be to introduce some additional functions like
>
> construct_PRNG(seed = 0L)
> destroy_PRNG()
>
> that the user would have to call explicitly before / after calling foo_rng(). That seemed difficult for me (to pass the reference to the PRNG to all _rng functions) and for users (who do not have to do that before / after calling functions like rnorm).
That would be most natural for the R users, I'd
think, with a default construction on entry.
> The way it works now is the way it has always worked, it is reproducible, and no one has mentioned it until today. And I don't know why anyone would want to call a _rng function multiple times with the same PRNG state within the same R session.
Agreed. And no point doing work on spec without a good motivation.
- Bob