Creating random sample larger that range

Viewed 78

I am trying to create a sample of 500 values between (and including) 60 and 126. ATM I've got:

random.sample(range(60,126),500)

but the syntax is invalid given the sample size is larger than my specified range.

Should I be using 'list' or some sort of iteration instead and if so, how?

2 Answers

You could use random.choices:

random.choices(range(60, 126), k=500)

This will return a list of 500 values where each value comes from the provided range. Values will be sampled from the same collection in each pick, so each value could appear more than once (even if the collection size is greater than k).

You can use numpy.random.randint for a faster and easier approach

np.random.randint(60, 127, size=500)
Related