title: Random dots inside a circle on a canvas
body: <p>I created a canvas and a circle on the canvas.</p>
  
  <p>I'm trying to make random dots inside the circle by using the solution <a href="https://stackoverflow.com/questions/19629872/canvas-for-drawing-is-a-circle-random-dots-within-canvas">here</a> but dots are being placed inside and outside the circle as well.</p>
  
  <p>here's my code from the moment I create the circle:</p>
  
  <pre><code> draw_circle(600, 600, 500);
  
  
              for (i = 0; i &lt; 20; i++) {
                   radius = 500;
                   y = 0;
                   x = 0;
                  y = -radius + Math.random() * (radius + radius + 1);
  
                  // x must respect x² + y² &lt; r²
                  xMax = Math.pow(Math.pow(radius, 2) - Math.pow(y, 2), 0.5);
                  x = Math.random() * 2 * xMax - xMax;
  
              draw_circle(x, y, 3);
              }
  </code></pre>
  
  <p>and this is my draw_circle function:</p>
  
  <pre><code> function draw_circle(x, y, r) {
  
           ctx.beginPath();
           ctx.arc(x, y, r, 0, Math.PI * 2);
           ctx.stroke();
       }
  </code></pre>