[R] Reconcile Random Samples
Daniel Nordlund
djnordlund at frontier.com
Thu Dec 23 20:36:45 CET 2010
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Mike Harwood
> Sent: Thursday, December 23, 2010 9:19 AM
> To: r-help at r-project.org
> Subject: [R] Reconcile Random Samples
>
> Is there a way to generate identical random samples using R's runif
> function and SAS's ranuni function? I have assigned the same seed
> values in both software packages, but the following results show
> different results. Thanks!
>
> R
> ===
> > set.seed(6)
> > random <- runif(10)
> > random
> [1] 0.6062683 0.9376420 0.2643521 0.3800939 0.8074834 0.9780757
> 0.9579337
> [8] 0.7627319 0.5096485 0.0644768
>
> SAS (the log file)
> =======
> 15 data _null_;
> 16 do i=1 to 10;
> 17 random = ranuni(6);
> 18 put i= random=;
> 19 end;
> 20 run;
>
> i=1 random=0.1097754189
> i=2 random=0.8205322939
> i=3 random=0.3989458365
> i=4 random=0.5563918723
> i=5 random=0.5296154672
> i=6 random=0.8156640985
> i=7 random=0.2578750389
> i=8 random=0.1901503369
> i=9 random=0.2987641572
> i=10 random=0.3993993096
>
Yes,
If you use the same pseudorandom number generator in both places. The ranuni() function in SAS uses a simple multiplicative generator
SEED = mod( SEED * 397204094, 2**31-1 )
and then returns the value SEED / (2^31 - 1) as the random number. SAS also has a much better generator, rand(). I would instead generate a series of random number in one program and export the numbers to the other program.
Hope this is helpful,
Dan
Daniel Nordlund
Bothell, WA
More information about the R-help
mailing list