The standard library random
module, and in particular random.sample
, is not really intended to work with numpy. Since you're using arrays, stick with numpy routines since they are generally much more efficient.
To generate the first array more simply, use np.random.uniform
:
x = np.random.uniform(1, 500, size=N)
You can sample the 100 items in a number of ways in numpy. A very basic method is to apply np.random.shuffle
to x
and slice off the first 100 elements:
np.random.shuffle(x)
sp_x = x[:100]
Another option is to usenp.random.choice
:
sp_x = np.random.choice(x, 100, replace=False)
All of the above is using the legacy API. The new Generator
API is preferred and generally much more efficient.
gen = np.random.default_rng()
x = gen.uniform(1, 500, size=N)
sp_x = gen.choice(x, 100, replace=False)
Keep in mind that using np.random.rand
, random.uniform
and random.Generator.uniform
selects floating point values on the interval [1, 500)
. If you wanted to generate integers in [1, 500]
instead, do something like
x = np.random.randint(1, 501, size=N)
Or
x = gen.integers(500, endpoint=True, size=N)