Is there a difference between...?

I want to simulate rolling a normal die 10000 times to demonstrate the Law of Large Numbers.
Is there a difference between using R to roll a die 10000 times and using R to replicate a single roll 10000 times? Which of the following codes would be more correct...or more intuitive?

d=c("H","T")
table(sample(d,10000,replace=TRUE)

vs.

d=c("H","T")
table(replicate(10000,sample(d,1)))

d=c("H","T")
set.seed(137)
table(sample(d,10000,replace = TRUE))
#> 
#>    H    T 
#> 5025 4975
set.seed(137)
table(replicate(10000,sample(d,1, replace = FALSE)))
#> 
#>    H    T 
#> 5025 4975
# use replacement
set.seed(137)
table(replicate(10000,sample(d,1,replace = TRUE)))
#> 
#>    H    T 
#> 5025 4975

Created on 2020-09-27 by the reprex package (v0.3.0.9001)

As a general rule, prefer the method employing the fewest functions.

Thank you so much! That was very helpful.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.