Unique random number sampling with Numpy

Viewed 2547

I need to create a 10,000 x 50 array in which each row contains an ascending series of random numbers between 1 and 365, like so:

[[  4  11  14 ..., 355 360 364]
 [  2  13  15 ..., 356 361 361]
 [  4  12  18 ..., 356 361 365]
 ..., 
 [  6   9  17 ..., 356 362 364]
 [  1  10  19 ..., 352 357 360]
 [  1   9  17 ..., 356 358 364]]

The only way I've figured out to do this is by way of an iterator:

sample_dates = np.array([np.sort(np.random.choice(365, 50, replace=False)) for _ in range(10000)])

which works, but is pretty slow (~0.33 seconds to run) and I'm going to be doing this thousands of times). Is there a faster way to accomplish this?

EDIT: From what I can tell, the most expensive part of this solution is the iteration and 10k individual calls to np.random.choice, not the sorting

4 Answers
Related