title: Generating Geometric variates without using inbuilt function in R
body: <p>I am learning R and have just started simulating from different probability distributions. I was trying to generate a random variate from the <strong>Geometric Distribution</strong>. I am not willing to use the inbuilt function <code>rgeom</code> in R for doing the same. Among other methods, I decided to use the result that:</p>
  <blockquote>
  <p>The <strong>Geometric distribution</strong> is the probability distribution of <strong>the number of Bernoulli trials needed to get one success</strong>.</p>
  </blockquote>
  <p>I wrote the function for generating one Bernoulli variable as <a href="https://stackoverflow.com/q/67434285/8484779">in my previous post</a> named as <code>ber</code>, with <code>p</code> being the success probability.</p>
  <p>Now to generate a Geometric variable, I can run a loop (I used a <code>repeat</code> loop here) and continuously generate a Bernoulli variable, check if it is a success or not and so on...</p>
  <p>I did it like the following:</p>
  <pre><code>geom&lt;-function(p)
  {
      x&lt;-1
      repeat
      {
          if(ber(p)==1) break
          x&lt;-x+1
      }
      return(x)
  }
  </code></pre>
  <p>One output as generated by <code>R</code> is:</p>
  <pre><code>geom(0.035)
  [1] 9
  </code></pre>
  <h2>What I want to do and where am I stuck:</h2>
  <p>I want to use a vectorized approach instead of this <code>repeat</code> loop method to generate a Geometric variable but I seem to have run out of ideas as to how that can be achieved. Any helps/suggestions and hints are very much welcome in here.</p>
  <p><strong>Note 1:</strong> As to why I defined a new function <code>geom(p)</code>, I originally planned to generate not one, but many (say <code>n</code>) Geometric variates. I figured that defining a function as like this, and then using the <code>replicate</code> function to generate more similar variables with the same success probability is easier.</p>
  <p><strong>Note 2:</strong> I really apologize if some of my previous posts seem similar (in the sense that asking for suggestion for replacing a loop with some vector methods), but I am a newbie in <code>R</code> and I am still learning about vectorized methods.</p>