title: How to write a function to generate a sequence of points in R?
body: <p>This is the PDF that I am dealing with: <br><br></p>
  
  <pre><code>fx = 0.3 if (0&lt;=x&lt;1)
  0.1 if (1&lt;=x&lt;2)
  0.25 if (2&lt;=x&lt;3)
  0.15 if (3&lt;=x&lt;4)
  0.2 if (4&lt;=x&lt;5)
  0 otherwise 
  </code></pre>
  
  <p>I have to write a function gen_xy that will generate a sequence of points (X, Y) uniformly distributed in (0, 5) X (0, 0.5) until one lies in the region under the curve of <code>y = fx(x)</code>.</p>
  
  <p>This is my code so far:</p>
  
  <pre><code>fx &lt;- function(x) c(0, 0.3,0.1,0.25,0.15,0.20, 0)
  [findInterval(x, c(-Inf, 0:5,  Inf))]
  
  x &lt;- runif(n,0,5)
  fx &lt;- stepfun(x = 0:5, y = c(0,0.3,0.1,0.25,0.15,0.20,0))
  plot(fx, ylim = c(0,0.5),xlim = c(0,5), main = 'f(x)')
  </code></pre>
  
  <p>Now this is my attempt at writing the code for my function:
  
  <pre><code>gen_xy &lt;- function() {
      done=0
      while(done==0) {
          x=runif(1,0,5)
           y=runif(1,0,0.5)
           print(c("x,y",c(x,y)))
           if(y &lt; fx(x)) {
               done=1
           }
      }
      xy=c(x,y)
      xy
  }
  </code></pre>
  
  <p>But I think the part <code>if(y &lt; fx)</code> is wrong?</p>
  
  <p>I then need to generate a sample of 1000 points and plot them to check if they are an appropriate sample from under the curve of <code>y=fx</code>. How would I go about writing such a code?</p>