From murdoch at stats.uwo.ca  Thu Jan  5 19:58:59 2006
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Thu, 05 Jan 2006 13:58:59 -0500
Subject: [Rd] random seed question
In-Reply-To: <58618a620601050844m5b7da38tb46f49488786122c@mail.gmail.com>
References: <58618a620601050844m5b7da38tb46f49488786122c@mail.gmail.com>
Message-ID: <43BD6C73.7080007@stats.uwo.ca>

On 1/5/2006 11:44 AM, Tib wrote:
> Greetings,
> 
> I am trying to write a C++ subroutine of my random number generator. Based
> on tutorial in Writing R Extensions, one should call GetRNGstate() before
> and PutRNGstate() after calling  R's random variate generation routines. Now
> suppose my function would generate n(n>1) numbers by a loop, do I need to
> call these two functions at each iteration? This certainly increases
> computation burden (although indiscernible in my case). But if I only call
> them once (outside the loop), will the quality of my random numbers be
> reduced because of serial correlations in RNG algorithms? I do comparisons
> between two methods, no significant difference is found. How do you guys
> write RNG functions? Any other specific reasons? Thanks,

Call them once, outside the loop.

What they do is move the RNG state between the R workspace and a place 
where the C functions can use it.  Wrapping each call in the loop in 
those calls will just generate a lot of unnecessary moves.  Not doing 
the calls, or not pairing them up properly, is the dangerous thing that 
would lead to damage to the RNG algorithms, because the state would not 
be updated properly.

Duncan Murdoch



[Rd] random seed question
Duncan Murdoch