0

I am trying to use the simulation to estimate the that all six faces appear exactly once in six tosses of fair die.

> nrep = 100000
> count = 0
for (i in 1:nrep) {
    x = sample(1:6, replace = TRUE)
    if () count = count + 1
}

What do I ask in the if statement?

I know how to ask the question for a one time roll but not a 6x roll.

Jimmy_Jump
  • 17
  • 5
  • Hi Jummy_Jump. Welcome to StackOverflow! Please read the info about [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and how to give a [minimale reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). That way you can help others to help you! – dario Feb 23 '20 at 13:19
  • `identical(sort(x), 1:6)` – dww Feb 23 '20 at 13:48
  • Assuming that `count` is used to count the number of successes (that is, the number of time that the six faces are obtained in a given iteration), you can use the following condition in the `if` statement: `if(length(unique(x))==6) count = count + 1`. – B. Christian Kamgang Feb 23 '20 at 13:57
  • Another possible is `count = count + !!length(setdiff(x, 1:6))` - but @B.ChristianKamgang 's version with `unique` is slightly faster. – dww Feb 23 '20 at 14:03
  • Yet another solution (without a for loop) is to use the `replicate` function. The time is about the same, but you only need one line: `sum(replicate(nrep, length(unique(sample(1:6, replace = TRUE)))==6))` – Edward Feb 23 '20 at 14:27

0 Answers0