For my research, I need to generate uniform random complex numbers. How to do this in python because there is no such module to generate the complex numbers.
Your question is underspecified, you need to say from what region of the complex plane you want to draw your uniformly distributed numbers.
For uniformly sampling real numbers, this is true as well.
However, in the real case there is a very obvious choice, namely the interval [0, 1).
You can see, for example that numpy.random.uniform
per default samples from this interval.
I will present some solution for regions of the complex plane that could be useful, but ultimately, the choice that is right for you will depend on your application.
Assume np
is numpy
and that we want to genereate an array of many such random numbers with shape shape
.
A square centered at the origin
I.e. sampling uniformly from all complex numbers z such that both real and imaginary part are in [-1,1]. You can generate such complex numbers e.g. via
np.random.uniform(-1, 1, shape) + 1.j * np.random.uniform(-1, 1, shape)
A disc centered at the origin
I.e. sampling uniformly from all complex numbers with absolute value in [0,1]. You can generate them e.g. as
np.sqrt(np.random.uniform(0, 1, shape)) * np.exp(1.j * np.random.uniform(0, 2 * np.pi, shape))
Explanation: We can parametrize points in the disc as z = r * exp(i a). Uniform distribution of z in the disc means that the angle a is uniform in [0, 2pi] but the radius is non-uniform (intuition: in the disc there are more points with larger radius than with a small one). the radius has a probability density of p(r) = 2r on the interval [0, 1], and a CDF (integral of p(r)) of F(r) = r^2, inverse CDF sampling then allows us to sample such radii as X = F^{-1}(Y) = sqrt(Y) where Y is uniformly distributed.
Is it not enough to do:
a = np.random.uniform(1,10,10)
b = a + a * <some constant>j
I think this stays uniform.
array([7.51553061 +9.01863673j, 1.53844779 +1.84613735j,
2.33666459 +2.80399751j, 9.44081138+11.32897366j,
7.47316887 +8.96780264j, 6.96193206 +8.35431847j,
9.13933486+10.96720183j, 2.10023098 +2.52027718j,
4.70705458 +5.6484655j , 8.02055689 +9.62466827j])
© 2022 - 2025 — McMap. All rights reserved.