Jivan Jivan - 23 days ago 6
Python Question

NumPy - select random list of unique integers within a range

Within

n = range(1, n+1)
I have to randomly generate a list of
n * p
unique integers.

For instance if
n = 10
and
p = 0.3
then possible results can be:

[2, 6, 9]
[1, 5, 7]
[3, 4, 8]
[3, 5, 6]
etc


The following Python code does the job perfectly:

import random

n = 250000
p = 0.8
np = int(n * p)

result = []
for i in range(np):
attempt = random.randint(1, n)
if attempt not in result:
result.append(attempt)


However, because it's Python, it can take long (like, more than one minute) for
np > 200000
.

Can you see a more efficient version of the above solution, using
NumPy
?

Answer

Try with this:

result = random.sample(range(1,n+1),p*n)

Link to the documentation of random.sample

Comments