# Is there a way to generate a data-driven Monte Carlo sample from a histogram?

I have a vector with 50 elements. These are values of a random variable. I have represented the data as Histogram[RandomVariable1Histogram]. However, I need to obtain a bigger sample from said histogram, about 1000 values. If I were working with a known distribution, lets say a Gamma, I could simply write

nsim = 1000;
newValues =


However, since I am currently dealing with an unknown random variable, I don't know how to sample said histogram. My only idea is to use HistogramDistribution and somehow put the output inside of RandomVariate. However, I am not very happy with this since it requires the calculation of a probability density function. Is there a better way of obtaining a sample of a random variable of which only a histogram is available?

You may do this using "SmoothKernelDistribution".

Here is a simple example. We first create some data from a Normal Distribution:

d = RandomVariate[NormalDistribution[], 100];


Then we get a smooth distribution from this data:

dist= SmoothKernelDistribution[d];


We can now test the outcome by sampling from this distribution and see if it is approximately normal:

rv = RandomVariate[dist, 1000];
Mean[rv]
Variance[rv]
Histogram[rv]


• Thanks for your answer. I have read the documentation of SmoothKernelDistribution and attempted empiricPDF = SmoothKernelDistribution[RandomVariable1Histogram] and d = RandomVariate[empiricPDF, 1000]; it seems to work if I write Histogram[d] Nov 25, 2021 at 11:25
• The answer needs to be modified. What is dist above?
– Asim
Nov 25, 2021 at 15:31
• This is exactly what one would do if the raw data is available. But I read that the OP only has the histogram (bins and bin counts).
– JimB
Nov 25, 2021 at 16:30
• From a histogram one can always create some data with multiple entries. Nov 25, 2021 at 16:40
• @Asim. Thanks, it looks like I copied the wrong line. I corrected it. Nov 25, 2021 at 16:41

This is precisely what RandomChoice does: choosing numbers according to weights. For example, if your histogram is

hist = {99, 217, 1026, 3};


then you can draw a random number from $$\{1,2,3,4\}$$ that is distributed similarly with

RandomChoice[hist -> Range[Length[hist]]]


or you can draw $$10^6$$ numbers simultaneously with

RandomChoice[hist -> Range[Length[hist]], 10^6]