Greetings. My wife is teaching an introductory stat class at UC Davis. The
class emphasizes the use of simulations, rather than mathematics, to get
insight into statistics, and R is the mandated tool.  A student in the class
recently inquired about different approaches to sampling from a binomial
distribution. I've appended some code that exhibits the idea, the gist of
which is that using sample(c(0, 1), ...) and rbinom(...) should give
equivalent results.
The surprising (to me) result is that the two approaches DO give the same
result, EXCEPT when the probability is exactly 0.5. See Appendix A for the
code and Appendix B for the output. I don't think this issue is
system-dependent, but I've put my session information in Appendix C.
Another wrinkle in this is that if I omit the "prob" parameter from the call
to sample, meaning to take the default value of 0.5, the two methods DO give
the same result.
Any thoughts about this? Thanks.
--Mike
Appendix A: some R code that exhibits the problem
=================================================
ppp <- seq(0, 1, by = 0.01)
result <- do.call(rbind, lapply(ppp, function(p) {
   set.seed(1)
   sampleRes <- sample(c(0, 1), size = 1, replace = TRUE,
                       prob=c(1-p, p))
  Â
   set.seed(1)
   rbinomRes <- rbinom(1, size = 1, prob = p)
  Â
   data.frame(prob = p, equivalent = all(sampleRes == rbinomRes))
  Â
}))
result
Appendix B: the output from the R code
======================================
   prob equivalent
1Â Â 0.00Â Â Â Â Â Â TRUE
2Â Â 0.01Â Â Â Â Â Â TRUE
3Â Â 0.02Â Â Â Â Â Â TRUE
4Â Â 0.03Â Â Â Â Â Â TRUE
5Â Â 0.04Â Â Â Â Â Â TRUE
6Â Â 0.05Â Â Â Â Â Â TRUE
7Â Â 0.06Â Â Â Â Â Â TRUE
8Â Â 0.07Â Â Â Â Â Â TRUE
9Â Â 0.08Â Â Â Â Â Â TRUE
10Â 0.09Â Â Â Â Â Â TRUE
11Â 0.10Â Â Â Â Â Â TRUE
12Â 0.11Â Â Â Â Â Â TRUE
13Â 0.12Â Â Â Â Â Â TRUE
14Â 0.13Â Â Â Â Â Â TRUE
15Â 0.14Â Â Â Â Â Â TRUE
16Â 0.15Â Â Â Â Â Â TRUE
17Â 0.16Â Â Â Â Â Â TRUE
18Â 0.17Â Â Â Â Â Â TRUE
19Â 0.18Â Â Â Â Â Â TRUE
20Â 0.19Â Â Â Â Â Â TRUE
21Â 0.20Â Â Â Â Â Â TRUE
22Â 0.21Â Â Â Â Â Â TRUE
23Â 0.22Â Â Â Â Â Â TRUE
24Â 0.23Â Â Â Â Â Â TRUE
25Â 0.24Â Â Â Â Â Â TRUE
26Â 0.25Â Â Â Â Â Â TRUE
27Â 0.26Â Â Â Â Â Â TRUE
28Â 0.27Â Â Â Â Â Â TRUE
29Â 0.28Â Â Â Â Â Â TRUE
30Â 0.29Â Â Â Â Â Â TRUE
31Â 0.30Â Â Â Â Â Â TRUE
32Â 0.31Â Â Â Â Â Â TRUE
33Â 0.32Â Â Â Â Â Â TRUE
34Â 0.33Â Â Â Â Â Â TRUE
35Â 0.34Â Â Â Â Â Â TRUE
36Â 0.35Â Â Â Â Â Â TRUE
37Â 0.36Â Â Â Â Â Â TRUE
38Â 0.37Â Â Â Â Â Â TRUE
39Â 0.38Â Â Â Â Â Â TRUE
40Â 0.39Â Â Â Â Â Â TRUE
41Â 0.40Â Â Â Â Â Â TRUE
42Â 0.41Â Â Â Â Â Â TRUE
43Â 0.42Â Â Â Â Â Â TRUE
44Â 0.43Â Â Â Â Â Â TRUE
45Â 0.44Â Â Â Â Â Â TRUE
46Â 0.45Â Â Â Â Â Â TRUE
47Â 0.46Â Â Â Â Â Â TRUE
48Â 0.47Â Â Â Â Â Â TRUE
49Â 0.48Â Â Â Â Â Â TRUE
50Â 0.49Â Â Â Â Â Â TRUE
51Â 0.50Â Â Â Â Â FALSE
52Â 0.51Â Â Â Â Â Â TRUE
53Â 0.52Â Â Â Â Â Â TRUE
54Â 0.53Â Â Â Â Â Â TRUE
55Â 0.54Â Â Â Â Â Â TRUE
56Â 0.55Â Â Â Â Â Â TRUE
57Â 0.56Â Â Â Â Â Â TRUE
58Â 0.57Â Â Â Â Â Â TRUE
59Â 0.58Â Â Â Â Â Â TRUE
60Â 0.59Â Â Â Â Â Â TRUE
61Â 0.60Â Â Â Â Â Â TRUE
62Â 0.61Â Â Â Â Â Â TRUE
63Â 0.62Â Â Â Â Â Â TRUE
64Â 0.63Â Â Â Â Â Â TRUE
65Â 0.64Â Â Â Â Â Â TRUE
66Â 0.65Â Â Â Â Â Â TRUE
67Â 0.66Â Â Â Â Â Â TRUE
68Â 0.67Â Â Â Â Â Â TRUE
69Â 0.68Â Â Â Â Â Â TRUE
70Â 0.69Â Â Â Â Â Â TRUE
71Â 0.70Â Â Â Â Â Â TRUE
72Â 0.71Â Â Â Â Â Â TRUE
73Â 0.72Â Â Â Â Â Â TRUE
74Â 0.73Â Â Â Â Â Â TRUE
75Â 0.74Â Â Â Â Â Â TRUE
76Â 0.75Â Â Â Â Â Â TRUE
77Â 0.76Â Â Â Â Â Â TRUE
78Â 0.77Â Â Â Â Â Â TRUE
79Â 0.78Â Â Â Â Â Â TRUE
80Â 0.79Â Â Â Â Â Â TRUE
81Â 0.80Â Â Â Â Â Â TRUE
82Â 0.81Â Â Â Â Â Â TRUE
83Â 0.82Â Â Â Â Â Â TRUE
84Â 0.83Â Â Â Â Â Â TRUE
85Â 0.84Â Â Â Â Â Â TRUE
86Â 0.85Â Â Â Â Â Â TRUE
87Â 0.86Â Â Â Â Â Â TRUE
88Â 0.87Â Â Â Â Â Â TRUE
89Â 0.88Â Â Â Â Â Â TRUE
90Â 0.89Â Â Â Â Â Â TRUE
91Â 0.90Â Â Â Â Â Â TRUE
92Â 0.91Â Â Â Â Â Â TRUE
93Â 0.92Â Â Â Â Â Â TRUE
94Â 0.93Â Â Â Â Â Â TRUE
95Â 0.94Â Â Â Â Â Â TRUE
96Â 0.95Â Â Â Â Â Â TRUE
97Â 0.96Â Â Â Â Â Â TRUE
98Â 0.97Â Â Â Â Â Â TRUE
99Â 0.98Â Â Â Â Â Â TRUE
100 0.99Â Â Â Â Â Â TRUE
101 1.00Â Â Â Â Â Â TRUE
Appendix C: Session information
===============================
> sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-redhat-linux-gnu (64-bit)
locale:
 [1] LC_CTYPE=en_US.UTF-8      LC_NUMERIC=C            Â
 [3] LC_TIME=en_US.UTF-8       LC_COLLATE=en_US.UTF-8  Â
 [5] LC_MONETARY=en_US.UTF-8   LC_MESSAGES=en_US.UTF-8 Â
 [7] LC_PAPER=C                LC_NAME=C               Â
 [9] LC_ADDRESS=C              LC_TELEPHONE=C          Â
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=CÂ Â Â Â Â Â
attached base packages:
[1] stats    graphics grDevices utils    datasets methods  base   Â
>
______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.