A much easier way to do this, and what Kevin probably was looking for, is
replicate().
replicate(100, sum(sample(1:6,10, replace=TRUE)))
gives 100 sums of 10 fair dice.
-thomas
On Mon, 8 Oct 2007, Alberto Monteiro wrote:
> Kevin Zembower wrote:
>>
>> I'm trying to get R to simulate
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, Zemb
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(s
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
ject: Re: [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
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,
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(s
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
8 matches
Mail list logo