I have a list of numbers:
data = [15, 30, 45]
how to generate a list of N numbers taken randomly from this data
list? To get result as:
new_data = [15,15, 30, 45, 15,45, 30, 15, 45, 30, 45, 45, 45, 15, ...]
np.random.randint(15, high=45, size=N) # does not help here
What numpy
functions to use for this?
You can simply use np.random.choice
:
import numpy as np
data=[15,30,45]
N = 50
new_list = np.random.choice(data,N)
Edit: Using random.sample()
won't work as the sampling is done without replacement, therefore samples can't exceed length of the original data.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments