[S] Problems in generating Random numbers with sample()

Smith, Stephen (SmithSJ@mar.dfo-mpo.gc.ca)
Mon, 25 May 1998 09:50:08 -0300


Over the weekend (it was raining) I attempted to do some "intuition"
checking
simulations and discovered some very strange behaviour in in S-PLUS 4.3.

Below are the main functions, transect.simulate and transect.sample. In
S-PLUS 3.3, the transect.sample program works as expected with ni
changing from replication to replication ensuring a "random" selection
from the population that I was sampling from. However, the very same
code in S-PLUS 4.3 resulted in the same ni being chosen for each
replication. I have given the relevant intermediate results for each
version below following the functions. Note that .Random.seed does not
change from rep to rep in the S-PLUS 4.3 version.

Any insight would be appreciated.

Stephen Smith

transect.simulate
function(pop.data, reps, sample.size)
{
out.sim <- matrix(0, reps, 6)
for(i in 1:reps) {
temp <- transect.sample(pop.data, sample.size)
out.sim[i, ] <- transect.estimate(temp)
}
class(out.sim) <- "transect.sim"
out.sim
}

> transect.sample
function(obj, sample.size)
{
ni <- sample(length(obj$yi), sample.size)
browser()
list(yi = obj$yi[ni], Length = obj$Length[ni], area = obj$area,
baseline = obj$baseline, e.width = obj$
e.width)
}

S-PLUS 3.3
-----------------------
> transect.simulate(pop.test.1,5,5)
Called from: transect.sample(pop.data, samp . . .
b(3)> ni
[1] 34 33 2 27 25
b(3)> .Random.seed
[1] 33 59 52 49 4 2 24 32 5 29 48 0
b(3)> 0
Called from: transect.sample(pop.data, samp . . .
b(3)> ni
[1] 18 33 30 20 14
b(3)> .Random.seed
[1] 25 60 19 62 51 1 17 8 46 54 21 0
b(3)> 0
Called from: transect.sample(pop.data, samp . . .
b(3)> ni
[1] 10 33 15 34 11
b(3)> .Random.seed
[1] 21 35 10 36 7 1 4 14 55 51 11 0
b(3)> 0

S-PLUS 4.3
---------------------------------

> transect.simulate(pop.test.1,5,5)
Called from: transect.sample(pop.data, samp . . .
b(6)> ni
[1] 34 33 2 27 25
b(6)> .Random.seed
[1] 33 59 52 49 4 2 24 32 5 29 48 0
b(6)> 0
Called from: transect.sample(pop.data, samp . . .
b(6)> ni
[1] 34 33 2 27 25
b(6)> .Random.seed
[1] 33 59 52 49 4 2 24 32 5 29 48 0
b(6)> 0
Called from: transect.sample(pop.data, samp . . .
b(6)> ni
[1] 34 33 2 27 25
b(6)> .Random.seed
[1] 33 59 52 49 4 2 24 32 5 29 48 0
b(6)> 0

---------------------------------------
Stephen J. Smith
Head, Molluscan Fisheries Section
Invertebrate Fisheries Division
Department of Fisheries and Oceans
P.O. Box 1006
Dartmouth, Nova Scotia
Canada B2Y 4A2

Street address for Couriers:
1 Challenger Drive

Voice: 902-426-3317
Fax: 902-426-1862
Note new email address.
Internet: Smithsj@mar.dfo-mpo.gc.ca

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news