Dear Lemarian, I was going to reply to say this is how I would do (almost) the same thing with the prob package:
library(prob) S <- rolldie(2, makespace = TRUE) tmp <- sim(S, ntrials = 10000) with(tmp, mean(X1 == X2)) but then it occurred to me that Greg's "dice" was much faster than "sim" (plus that graph of the die faces is really cool!) so I was just going to let it go. But then again it occurred to me that simulation isn't needed for the problem at all: prob(S, X1 == X2) But finally as a reality-check it occurred to me that for a problem like this using R is like shooting a squirrel with a ballistic missile. Not that there's anything wrong with that; I don't like squirrels. Cheers, Jay P.S. Just kidding. On Tue, Oct 5, 2010 at 12:06 PM, Greg Snow <greg.s...@imail.org> wrote: > I would do it like this: > > library(TeachingDemos) > tmp <- dice(10000, 2) > with(tmp, c(sum(Red==Green),mean(Red==Green)) ) > plot(head(tmp,28)) > > > > > -- > Gregory (Greg) L. Snow Ph.D. > Statistical Data Center > Intermountain Healthcare > greg.s...@imail.org > 801.408.8111 > > >> -----Original Message----- >> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- >> project.org] On Behalf Of Lemarian WallaceIII >> Sent: Monday, October 04, 2010 10:14 PM >> To: r-help@r-project.org >> Subject: [R] R-help >> >> Im trying to simulate the rolling of a pair of dice >> >> this is my function: >> #function to simulate tosses of a pair of dice >> #from the simulation, the program returns the empirical probability of >> #observing a double >> count <- 0 >> for(j in 1:sim){#begin loop >> die1 <- sample(1:6,1) >> print(die1) >> die2 <- sample(1:6,1) >> print(die2) >> count <- ifelse(die1 == die2, count + 1, count) >> }#end loop >> emprob <- count/sim >> return(count,emprob) >> } #end program >> >> >> these are the errors that keep coming up: >> Error in 1:sim : 'sim' is missing >> >> >> How do I correct this? >> >> >> >> [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- *************************************************** G. Jay Kerns, Ph.D. Associate Professor Department of Mathematics & Statistics Youngstown State University Youngstown, OH 44555-0002 USA Office: 1035 Cushwa Hall Phone: (330) 941-3310 Office (voice mail) -3302 Department -3170 FAX VoIP: gjke...@ekiga.net E-mail: gke...@ysu.edu http://people.ysu.edu/~gkerns/ ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.