Generate uniform distribution of points in a circle

I want to generate points in uniform distribution in a circle. So I am using equations to get the coordinates of the points. When I am using those equations in Python, I am obtaining correct results. But, when I am using the same equations in C++, I am getting distribution in a square instead of a circle…
I don’t know what is going on.

Here is the code in Python to generate uniform distribution in a circle:

import matplotlib.pyplot as plt
import numpy as np

circle_radius = 1
num = 1000

theta = np.random.uniform(0.0, 2.0*np.pi, num)
zero_to_one = np.random.uniform(0.0, 1.0, num)

x = circle_radius * np.sqrt(zero_to_one) * np.cos(theta)
y = circle_radius * np.sqrt(zero_to_one) * np.sin(theta)

plt.plot(x, y, "ro", ms=1)
plt.axis([-2.0, 2.0, -2.0, 2.0])
plt.show()

When I run this, I am obtaining:

Now, lets go to C++. Here is the code:

#include <iostream>
#include <random>

#define M_PI 3.1415926f

int main()
{
	std::mt19937 generator(std::random_device{}());
	std::uniform_real_distribution<float> zero_to_one(0.0f, 1.0f);
	std::uniform_real_distribution<float> theta(0.0f, 2 * M_PI);

	float circle_radius = 1.0f;
	const int number_of_points = 1000;

	for (size_t i = 0; i < number_of_points; ++i)
	{
		float x = circle_radius * sqrt(zero_to_one(generator)) * cos(theta(generator));
		float y = circle_radius * sqrt(zero_to_one(generator)) * sin(theta(generator));
	}

}

When I copy the points coordinates generated using the above code to Python’s script, which plots points, I am getting:

Why?

Here is the Python code to plot points of given coordinates:

import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

x=[0.2, 1, 0.5, -0.5]
y=[0.2, 1, 0.5, -0.5]

plt.plot(x, y, 'r*')
plt.axis([-2.0, 2.0, -2.0, 2.0])

for i, j in zip(x, y):
   plt.text(i, j+0.5, ''.format(i, j))

plt.show()

This is very strange.

The radius and angle used for x aren’t the same values which are used for y. For each point, you should call zero_to_one(generator) and theta(generator) once each and store the results in variables which are used to calculate x and y.

Also: this question has absolutely nothing to do with OpenGL.

1 Like

Thank you. It is working now. I should have figured it out myself.
I am using this for my OpenGL project, but indeed the question is not related with OpenGL itself. I just knew, that you will help me (because OpenGL users have experience with this kind of problems), hence I posted it here. This topic can be deleted now. Thank you again.