lundi 7 mai 2018

Efficient Way to create a 2d array of random shuffling of a range of numbers

I need to create a N x M numpy array of numbers 0 to N.

The best I've been able to come up with so far is (for N = 10 and M = 5)

import random
import numpy as np

a = np.array([random.sample(range(10), 10) for x in range(5)])

which gives me

[[5 9 1 3 8 2 6 4 0 7]
 [4 8 5 2 9 3 7 6 0 1]
 [8 4 6 7 9 2 0 5 1 3]
 [3 5 4 9 2 0 6 7 1 8]
 [6 0 4 7 3 2 1 8 5 9]]

I'm assuming numpy has an easier way to do this, that I'm currently missing.

Aucun commentaire:

Enregistrer un commentaire