If I understand correctly, you have a continuous variable whose possible values are partitioned into bins. And, you have a histogram, giving a count value for each bin. This defines a continuous probability distribution, whose density function is constant in each bin. Here's how this distribution is defined, and how to sample from it.
The PDF
Suppose there are $k$ bins. Let $n_i$ be the count assigned to the $i$th bin, and let $a_i$ and $b_i$ be its left and right edges (so its width is $b_i - a_i$). The overall probability mass assigned to each bin is given by its count, divided by the total count:
$$p_i = \frac{n_i}{\sum_{j=1}^k n_j}$$
The probability mass in each bin is spread uniformly over its width. So, the probability density at any point $x$ is given by the probability mass of the bin containing it, divided by the bin width (or $0$ if $x$ lies outside all bins):
$$p(x) = \left\{ \begin{array}{cl}
\frac{p_1}{b_1-a_1} && x \in \text{bin } 1 \\
& \vdots & \\
\frac{p_k}{b_k-a_k} && x \in \text{bin } k \\
0 && \text{Otherwise}
\end{array} \right.$$
Sampling
To sample a point $x$ from this distribution:
- Randomly select a bin according to its probability mass. That is, sample an integer $j \in \{1, \dots, k\}$ from a categorical distribution with probabilities $[p_1, \dots, p_k]$:
$$j \sim Cat(p_1, \dots, p_k)$$
- Sample $x$ from a uniform distribution over the chosen bin:
$$x \sim U(a_j, b_j)$$
Note
Points generated as above are samples from the given histogram. As such, they will recapitulate the moments (and other properties) of the histogram itself. But, an important distinction arises if the histogram was obtained by quantizing some underlying distribution, or fit to data that was generated by some underlying distribution. In this case, the histogram only approximates the underlying distribution. And, as whuber has pointed out, moments of the histogram (and samples from it) may systematically differ from those of the underlying distribution. See here for more information.