rishiag
rishiag

Reputation: 2274

What will be the optimized and fast way to generate 1 million random points in Python?

Basically I need over a million points on x-y plane. I am thinking about first generating 1 million points in range -x to x, another million in range y to -y and then coupling them together. What will be optimized and fast way to do this? In random.randrange good enough?

Upvotes: 0

Views: 202

Answers (1)

NPE
NPE

Reputation: 500267

I would use NumPy for that. In my quick test numpy.random.uniform() is over 60 times faster than calling random.randrange() in a loop.

In [12]: %timeit [random.randrange(-10, 10) for _ in range(2000000)]
1 loops, best of 3: 2.95 s per loop

In [13]: %timeit numpy.random.uniform(-10, 10, (1000000, 2))
10 loops, best of 3: 43.8 ms per loop

Upvotes: 1

Related Questions