Simulating a Coin toss I am trying to simulate a coin toss using R. For that I used both sample function and rbinom function.
But I am getting different results.
> set.seed(1)
> rbinom(10,1,0.5)
 [1] 0 0 1 1 0 1 1 1 1 0

> set.seed(1)
> sample(c(0,1), 10, replace = TRUE)
 [1] 0 1 0 0 1 0 0 0 1 1

What may be the reason for that?
I thought for the same seed value, both sample function and rbinom should give the same result.
 A: An historical note about sample is that it got recently modified for being biased in some extreme situations (as commented by Chris Haug). In earlier versions of R such as 3.4.4, still running on my ownlaptop, the outcome of the above would be the same as a cdf inversion (and as the complement of the standard Uniform draw):
> set.seed(1)
> rbinom(10,1,0.5)
 [1] 0 0 1 1 0 1 1 1 1 0

> set.seed(1)
> sample(c(0,1), 10, replace = TRUE)
 [1] 0 0 1 1 0 1 1 1 1 0

> set.seed(1)
> qbinom(runif(10),1,0.5) #inverse cdf
 [1] 0 0 1 1 0 1 1 1 1 0

> set.seed(1)
> 1*(runif(10)>0.5) #complement!
 [1] 0 0 1 1 0 1 1 1 1 0

When checking the C code behind the R function rbinom, the adopted approach relies on a single Uniform when $np<30$:
/* inverse cdf logic for mean less than 30 */
repeat {
 ix = 0;
 f = qn;
 u = unif_rand();
 repeat {
 if (u < f)
     goto finis;
 if (ix > 110)
     break;
 u -= f;
 ix++;
 f *= (g / ix - r);

and a much more involved resolution otherwise ($np\ge 30$), resolution including an accept-reject step,
/*------- np = n*p >= 30 : ----- */
repeat {
  u = unif_rand() * p4;
  v = unif_rand();

hence a random number of Uniforms. On the other hand, sample (in a version of 2017!) uses a cascade of C functions:
if (replace) {
    int i, nc = 0;
    for (i = 0; i < n; i++) if(n * p[i] > 0.1) nc++;
    if (nc > 200)
    walker_ProbSampleReplace(n, p, INTEGER(x), k, INTEGER(y));
    else
    ProbSampleReplace(n, p, INTEGER(x), k, INTEGER(y));
} else
    ProbSampleNoReplace(n, p, INTEGER(x), k, INTEGER(y));

For instance, ProbSampleReplace is based on a single Uniform call:
/* compute the sample */
for (i = 0; i < nans; i++) {
  rU = unif_rand();
  for (j = 0; j < nm1; j++) {
      if (rU <= p[j])

and the other ones as well, which is not to say that they return the same outcome than rbinom!
A: I tried getting to the source code for both, but couldn't find it.
I did, however, find the references for the building of the two algorithms.  They do not use the same references, so it is reasonable that they do not generate them in a similar manner.  Indeed, sample() function briefly says it uses an easier way to handle random numbers, presumably through different generation.
