Jivan
Jivan

Reputation: 23038

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?

Upvotes: 2

Views: 2919

Answers (2)

quantummind
quantummind

Reputation: 2136

Try with this:

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

Link to the documentation of random.sample

Upvotes: 8

tomasn4a
tomasn4a

Reputation: 615

making result a set instead of a list will keep you from having to run through every item in the list at each iteration to check if the new item you are about to append already exists. Small improvement, but you should see a difference in performance.

Upvotes: 0

Related Questions