title: Randomly generate clustered points given a center coordinate in 3D
body: <p>I would like to be able to generate a cluster of points in 3D space that would create a majority of the points within a specified sphere radius (in this case, 4) from the starting point (in this case, 0,0,0). I'd also like it to generate the occasional outlier too.</p>
  
  <p>It would be similar to this:</p>
  
  <p><img src="https://i.stack.imgur.com/hROLa.png" alt="3D Point Clustering"></p>
  
  <p>But I'd like to be able to tweak some of the settings. I will be generating this in C# and I'm familiar with the Random class, and have some familiarity with spheres. I'm not sure how I can piece it all together.</p>
  
  <p>I have this which can generate a point in a sphere, but I want it to cluster around that center point:</p>
  
  <pre><code>// elsewhere in code
  private static readonly Random Random = new Random();
  
  private static double NextDouble(double minValue, double maxValue)
  {
      double next = Random.NextDouble();
  
      return minValue + (next * (maxValue - minValue));
  }
  
  // generates random points in sphere
  const int r = 6;
  
  double x;
  double y;
  double z;
  
  do
  {
      x = NextDouble(-r, 1 + r);
      y = NextDouble(-r, 1 + r);
      z = NextDouble(-r, 1 + r);
  
  } while (Math.Pow(x, 2) + Math.Pow(y, 2) + Math.Pow(z, 2) &gt; Math.Pow(r, 2));
  </code></pre>