i need to make fast numpy array that generates random integers in each row with a different range.
Code that works by my but is slow when i increase vectors number to 300000:
import numpy as np
import random
population_size = 4
vectors_number = population_size * 3
add_matrix = []
for i in range(0, int(vectors_number/population_size)):
candidates = list(range(population_size*i, population_size*(i+1)))
random_index = random.sample(candidates, 4)
add_matrix.append(random_index)
winning_matrix = np.row_stack(add_matrix)
print(winning_matrix)
Each row is choose 4 random numbers from variable range.
Output:
[[ 3 0 1 2]
[ 4 6 7 5]
[11 9 8 10]]
Best would by create that matrix using only numpy without loops
Revisiting the question with better understanding, realizing only the first random 4 of a 64 population are needed for the result, I came to this answer. There is still a loop but it is a loop over small number of required columns, it basically swaps the only the first 4 (FINALIST) columns with a a random other column:
Here's a vectorized approach following
this trick
to extract unique random samples -We can also leverage
np.argpartition
to replace the last step -Timings -
Since we are only choosing
4
out of64
collisions will be rare, we can therefore draw with replacement and correct afterwards.Sample run:
In your case the loops can be compressed using
map
and list comprehensions.Output:
This can be broken down as
In case of generation of random integers with different ranges (and not from a sample), you can follow the below method.
How about something like this
Output:
The rows will have random integers between range