Skip to content

Question on RNG

7 messages · Ron Michael, Duncan Murdoch, Barry Rowlingson +1 more

#
Hi all, I have happened to work on MS .NET for sometime now, and I found that this language offers RNG what is called as Donald E. Knuth's subtractive random number generator algorithm (found here: http://msdn.microsoft.com/en-us/library/system.random.aspx#Y12).
?
Here I was wondering whether R also have same RNG in it's inventory, so looked at ?set.seed. There I found 2 related RNGs namely 'Knuth-TAOCP-2002', 'Knuth-TAOCP'. Can somebody guide me what is the most related RNG with .Net's?
?
I also want to have R to draw random number from that RNG, given I set a seed. My goal is to have same set of random numbers in .Net & R, so that I can match and see and compare?the performance for some further calculations, which are based on random numbers in both .Net & R.
?
It will be really great to me if somebody can help me out.
?
Thanks,
#
On 11-08-05 2:17 PM, Ron Michael wrote:
As far as I know, Microsoft doesn't publish the code for their RNG. 
Naming it is not enough -- implementations that differ in subtle details 
will give different streams.

So I'd recommend that you call a known RNG from .NET, rather than 
whatever is built in.  R provides ways to call its RNGs from C, so it's 
probably possible to call it from .NET:  then you can be sure the two 
streams are identical.

Duncan Murdoch
#
Thanks Duncan for your reply. However I could not understand this: 'R provides ways to call its RNGs from C'. Can you be more detailed what I need to do here? 
?
Thanks,


----- Original Message -----
From: Duncan Murdoch <murdoch.duncan at gmail.com>
To: Ron Michael <ron_michael70 at yahoo.com>
Cc: "r-help at r-project.org" <r-help at r-project.org>
Sent: Saturday, 6 August 2011 1:24 AM
Subject: Re: [R] Question on RNG
On 11-08-05 2:17 PM, Ron Michael wrote:
As far as I know, Microsoft doesn't publish the code for their RNG. Naming it is not enough -- implementations that differ in subtle details will give different streams.

So I'd recommend that you call a known RNG from .NET, rather than whatever is built in.? R provides ways to call its RNGs from C, so it's probably possible to call it from .NET:? then you can be sure the two streams are identical.

Duncan Murdoch
#
On Fri, Aug 5, 2011 at 8:54 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:

            
Sometimes these subtle software differences are known by another name - "bugs".
The chatter on the .net site makes me think you'll be very lucky to
get the same numbers out of R as .Net - Microsoft say its a 'modified'
version of a Knuth algorithm (only MS would be so arrogant) and then
there's a bunch of MS programmers complaining about things not
working. Use with care...

Barry
#
On 11-08-05 4:04 PM, Ron Michael wrote:
If you were writing in C, then instructions would be in the Writing R 
Extensions manual (in section 6.3).  But you're using .NET, so you've 
got another step which I can't help you with:  how does a .NET program 
make a call to a C library?

Duncan Murdoch
#
On Fri, Aug 05, 2011 at 11:47:49PM +0530, Ron Michael wrote:
Hi.

R uses Mersenne Twister generator as the default one. There are
implementations of this generator in different laguages, including C#.
Links may be found on Wikipedia and also at the home page of
Mersenne Twister

  http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html

Different implementations of Mersenne Twister differ in some
details, in particular in the initialization algorithm.
In order to obtain the same stream of numbers, it will be
needed either to copy the initialization algorithm from R
to the C# implementation or vice versa. I can provide more
detail off-list, if you are interested.

Petr Savicky.
#
On Sat, Aug 06, 2011 at 09:37:22PM +0200, Petr Savicky wrote:
In fact, copying the code is not needed. CRAN package randtoolbox
contains Mersenne Twister with the initialization from 2002, which
is used also in the implementations mentioned above. Search
"runifInterface" or "set.generator()" in the documentation of
randtoolbox for more information. Base R started to use Mersenne
Twister before 2002 (i think, approximately in 2000), so it uses
a different initialization.

Hope this helps.

Petr Savicky.