Zembower, Kevin
2007-Oct-08 17:50 UTC
[R] Dice simulation: Getting rep to re-evaluate sample()?
I'm trying to get R to simulate the sum of the values on 10 fair dice (yes, it's related to a homework problem, but is not the problem itself). I tried to do this:> rep(sum(sample(1:6,100,replace=T)), times=10)[1] 341 341 341 341 341 341 341 341 341 341 and noticed that sum(sample()) seems to be only evaluated once. How can I overcome this, so that I get a vector of values that correspond to independent throws of 10 dice each time? Thanks for your advice and suggestions. -Kevin Kevin Zembower Internet Services Group manager Center for Communication Programs Bloomberg School of Public Health Johns Hopkins University 111 Market Place, Suite 310 Baltimore, Maryland 21202 410-659-6139
Charles C. Berry
2007-Oct-08 17:56 UTC
[R] Dice simulation: Getting rep to re-evaluate sample()?
See ?replicate which I think is what you are after. Chuck On Mon, 8 Oct 2007, Zembower, Kevin wrote:> I'm trying to get R to simulate the sum of the values on 10 fair dice > (yes, it's related to a homework problem, but is not the problem > itself). I tried to do this: >> rep(sum(sample(1:6,100,replace=T)), times=10) > [1] 341 341 341 341 341 341 341 341 341 341 > > and noticed that sum(sample()) seems to be only evaluated once. How can > I overcome this, so that I get a vector of values that correspond to > independent throws of 10 dice each time? > > Thanks for your advice and suggestions. > > -Kevin > > Kevin Zembower > Internet Services Group manager > Center for Communication Programs > Bloomberg School of Public Health > Johns Hopkins University > 111 Market Place, Suite 310 > Baltimore, Maryland 21202 > 410-659-6139 > > ______________________________________________ > R-help at 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Prof Brian Ripley
2007-Oct-08 18:10 UTC
[R] Dice simulation: Getting rep to re-evaluate sample()?
See ?replicate, e.g. replicate(sum(sample(1:6,100,replace=TRUE)), n=10) Function arguments are (in gneral) evaluated as if they were evaluated when passed to the function. On Mon, 8 Oct 2007, Zembower, Kevin wrote:> I'm trying to get R to simulate the sum of the values on 10 fair dice > (yes, it's related to a homework problem, but is not the problem > itself). I tried to do this: >> rep(sum(sample(1:6,100,replace=T)), times=10) > [1] 341 341 341 341 341 341 341 341 341 341 > > and noticed that sum(sample()) seems to be only evaluated once. How can > I overcome this, so that I get a vector of values that correspond to > independent throws of 10 dice each time?> Kevin Zembower > Internet Services Group manager > Center for Communication Programs > Bloomberg School of Public Health > Johns Hopkins University-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Richard M. Heiberger
2007-Oct-08 18:16 UTC
[R] Dice simulation: Getting rep to re-evaluate sample()?
rep( sum(sample(1:6,100,replace=T)), times=10) Read carefully what you wrote. You asked R to sum a single sample, then make ten copies of the sum. You need to take ten samples, then sum each. apply() would be helpful. -----Original Message----- I'm trying to get R to simulate the sum of the values on 10 fair dice (yes, it's related to a homework problem, but is not the problem itself). I tried to do this:> rep(sum(sample(1:6,100,replace=T)), times=10)[1] 341 341 341 341 341 341 341 341 341 341
Alberto Monteiro
2007-Oct-08 18:42 UTC
[R] Dice simulation: Getting rep to re-evaluate sample()?
Kevin Zembower wrote:> > I'm trying to get R to simulate the sum of the values on 10 fair dice > (yes, it's related to a homework problem, but is not the problem > itself). I tried to do this: > > rep(sum(sample(1:6,100,replace=T)), times=10) > [1] 341 341 341 341 341 341 341 341 341 341 >rep(stuff, times=10) will just take stuff and repeat 10 times. stuff is constant (but every time you repeat that line you get a different stuff to be repeated), so you get a vector of 10 equal numbers.> I overcome this, so that I get a vector of values that correspond to > independent throws of 10 dice each time? ># get 10*100 random d6s x <- sample(1:6, 10*100, replace=T) # transform into a 10 x 100 matrix y <- matrix(x, 10, 100) # sum the cols z <- colSums(y) Of course, you can combine these three lines into one. Alberto Monteiro
Julian M Burgos
2007-Oct-08 21:04 UTC
[R] Dice simulation: Getting rep to re-evaluate sample()?
Well, if the dice are fair, then all rolls are independent. If you want to roll each dice 100 times, you can do something like rolls=matrix(sample(1:6,1000,replace=T),ncol=10) apply(rolls,2,sum) I'm sure that there must be a more elegant way to do it, though. Julian On Mon, 8 Oct 2007, Zembower, Kevin wrote:> I'm trying to get R to simulate the sum of the values on 10 fair dice > (yes, it's related to a homework problem, but is not the problem > itself). I tried to do this: >> rep(sum(sample(1:6,100,replace=T)), times=10) > [1] 341 341 341 341 341 341 341 341 341 341 > > and noticed that sum(sample()) seems to be only evaluated once. How can > I overcome this, so that I get a vector of values that correspond to > independent throws of 10 dice each time? > > Thanks for your advice and suggestions. > > -Kevin > > Kevin Zembower > Internet Services Group manager > Center for Communication Programs > Bloomberg School of Public Health > Johns Hopkins University > 111 Market Place, Suite 310 > Baltimore, Maryland 21202 > 410-659-6139 > > ______________________________________________ > R-help at 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. >