Jivan - 23 days ago 6

Python Question

Within

`n = range(1, n+1)`

`n * p`

For instance if

`n = 10`

`p = 0.3`

`[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

Source (Stackoverflow)

Comments