title: Acceptance-rejection for beta distribution R code
body: <p>I’m using the acceptance-rejection method for beta distribution with g(x) = 1, 0 ≤ x ≤ 1.
  The function is:
  f(x) = 100x^3(1-x)^2.</p>
  
  <p>I want to create an algorithm to generate data from this density function.</p>
  
  <p>How can I estimate P(0 ≤ X ≤ 0.8) with k = 1000 repetitions (n=1000)? How can I solve this in R? </p>
  
  <p>I already have:</p>
  
  <pre><code>beta.rejection &lt;- function(f, c, g, n) {
  naccepts &lt;- 0
  result.sample &lt;- rep(NA, n)
  
    while (naccepts &lt; n) {
      y &lt;- runif(1, 0, 0.8)
      u &lt;- runif(1, 0, 0.8)
  
      if ( u &lt;= f(y) / (c*g(y)) ) {
        naccepts &lt;- naccepts + 1
        result.sample[n.accepts] = y
      }
    }
  
    result.sample
  }
  
  f &lt;- function(x) 100*(x^3)*(1-x)^2
  g &lt;- function(x) x/x
  c &lt;- 2
  
  result &lt;- beta.rejection(f, c, g, 10000)
  
  for (i in 1:1000){ # k = 1000 reps
    result[i] &lt;- result[i] / n
  }
  
  print(mean(result))
  </code></pre>