Your code already looks good to me, but here are a few more thoughts.
.
, .
def randvector2(n):
return np.exp((2.0j * np.pi) * np.random.rand(n, 1)).view(dtype=np.float64)
n = 10000
Yours:
1000 loops, best of 3: 716 Β΅s per loop
:
1000 loops, best of 3: 834 Β΅s per loop
, , .
, hstack.
.
, .
def randvector3(n):
x = np.empty([n,2])
theta = (2 * np.pi) * np.random.rand(n)
np.cos(theta, out=x[:,0])
np.sin(theta, out=x[:,1])
return x
:
1000 loops, best of 3: 698 Β΅s per loop
numexpr, ( , ).
import numexpr as ne
def randvector3(n):
sample = np.random.rand(n, 1)
c = 2.0j * np.pi
return ne.evaluate('exp(c * sample)').view(dtype=np.float64)
:
1000 loops, best of 3: 366 Β΅s per loop
, -, , , .
.
hstack .
:
n = 10, .
n = 10000000, pure-numpy .