lundi 21 mai 2018

Create numpy array with random integers each row with another range

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




Aucun commentaire:

Enregistrer un commentaire