[R] Dice simulation: Getting rep to re-evaluate sample()?
Prof Brian Ripley
ripley at stats.ox.ac.uk
Mon Oct 8 20:10:09 CEST 2007
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
More information about the R-help
mailing list