[R] Dice simulation: Getting rep to re-evaluate sample()?
kzembowe at jhuccp.org
Mon Oct 8 20:11:01 CEST 2007
Thanks so much, Chuck and Mark. Here's my script to simulate 10,000
rolls of 100 fair dice to demonstrate their conformity to a normal
> x<-replicate(10000, sum(sample(1:6,100,replace=T)))
> hist(x, freq=FALSE)
> curve(dnorm(x, mean=meanx, sd=sdx), add=TRUE)
Thanks, again, for your quick and accurate help.
From: Charles C. Berry [mailto:cberry at tajo.ucsd.edu]
Sent: Monday, October 08, 2007 1:56 PM
To: Zembower, Kevin
Cc: R-help at stat.math.ethz.ch
Subject: Re: [R] Dice simulation: Getting rep to re-evaluate sample()?
which I think is what you are after.
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)
>  341 341 341 341 341 341 341 341 341 341
> and noticed that sum(sample()) seems to be only evaluated once. How
> 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 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
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
Charles C. Berry (858) 534-2098
Dept of Family/Preventive
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego
More information about the R-help