vendredi 22 décembre 2017

Drawing random numbers with draws in some pre-defined interval, `numpy.random.choice()`

I would like to use numpy.random.choice() but make sure that the draws are spaced by at least a certain "interval":

As a concrete example,

import numpy as np
np.random.seed(123)
interval = 5
foo = np.random.choice(np.arange(1,50), 5)  ## 5 random draws between array([ 1,  2, ..., 50])
print(foo)
## array([46,  3, 29, 35, 39])

I would prefer these be spaced by at least the interval+1, i.e. 5+1=6. In the above example, there should be another random draw, as 35 and 39 are separated by 4, which is less than 6.

The array array([46, 3, 29, 15, 39]) would be ok, as all draws are spaced by at least 6.

numpy.random.choice(array, size) draws size number of draws in array. Is there another function used to check the "spacing" between elements in a numpy array? I could write the above with an if/while statement, but I'm not sure how to most efficiently check the spacing of elements in a numpy array.




Aucun commentaire:

Enregistrer un commentaire