I am trying to generate a sample of 2000 rows. I have the following values min = 80 max = 12000 mean = 500

I want to generate only positive numbers. I tried using triangular distribution and range rule(sd = max-min/4). The values were negative.

Is there anyway I can generate only positive numbers?

  • 2
    $\begingroup$ What kind of distribution of values do you want? $\endgroup$
    – Dave
    Jul 26, 2019 at 17:35
  • $\begingroup$ as long as they are positive and has mean = 500, min = 80 and max = 12000 $\endgroup$ Jul 26, 2019 at 17:36
  • $\begingroup$ min and max is fine but the mean is way too off in triangular distribution, do you have any other suggestion? $\endgroup$ Jul 26, 2019 at 17:37
  • $\begingroup$ Strongly related: stats.stackexchange.com/q/236449/35989 $\endgroup$
    – Tim
    Jul 26, 2019 at 18:44

3 Answers 3


While the problem is very much ill-posed, since there is an infinite range of distributions satisfying these constraints, a possible solution is to find the maximum entropy distribution under the constraint of a support of $(80,12000)$ [thus using the uniform measure on that interval as the reference measure] and a mean of $\mathbb E[X]=500$ is of the form $$p(x)=\exp\{\alpha+\beta x\}\,\mathbb I_{(80,1200)}(x)$$ with $$\int_{80}^{12000} \exp\{\alpha+\beta x\}\,\text dx=1\qquad\text{and}\qquad \int_{80}^{12000} x\exp\{\alpha+\beta x\}\,\text dx=500$$ which leads to $$\exp\{-\alpha\}=\beta^{-1}[\exp\{12000\beta\}-\exp\{80\beta\}]$$ and$$\beta^{-1}\exp\{\alpha\}[12000\exp\{12000\beta\}-80\exp\{80\beta\}]-\beta^{-1}=500$$which can be solved numerically in $\beta$. Leading to $$\beta^*=-.00238\quad\text{and}\quad\alpha^*=-5.850$$which can be easily simulated as a truncated exponential distribution, by inversion of the cdf, e.g., using qexp() in R. For instance,


If the question is instead about simulating a sample $X_{1:2000}$ such that $$\min(X_{1:2000})=80,\quad\max(X_{1:2000})=1200,\quad\bar X_{1:2000}=500$$ there is again an infinite range of solutions, the simplest being a uniform Multinomial distribution constrained by its minimum $X_{(1)}$ being 80 and its maximum $X_{(2000)}$ being 12000 since $$\underbrace{X_{(1)}}_{80}+\cdots+\underbrace{X_{(2000)}}_{12000} = 80 + 987920 + 12000= \underbrace{2000}_p\times 500=\underbrace{10^6}_n$$ namely proportional to $${n\choose 80\,n_2\,\cdots\,n_{p-1}\,12000}\mathbb I_{80\le n_1\le\ldots\le n_{p-1}\le 1200}$$ This is equivalent to simulate a Multinomial $$\mathcal M_{1998}(987920,1/1998,\ldots,1/1998)$$ constrained to $(80,1200)^{1998}$, ie

while (min(x)<80||max(x)>12000){

As an additional remark, let me add that observing a range of (80,12000) for a Multinomial $\mathcal M(10⁶;2000)$ is extremely unlikely (in the above simulation, the first attempt is always successful) and a more satisfactory approach would be to infer first about the probability vector of a Multinomial $\mathcal M(10⁶;2000;p)$ before predicting the remaining 1998 categories.

  • $\begingroup$ Is your comment about $X_{1:2000}$ about ensuring that the sample (not the population) has a mean of 500? Also, two clarifications would be helpful. 1) Why do something in the form of $exp\{\alpha + \beta X\}$? 2) Once we solve for $\alpha$ and $\beta$, how do to simulate draws from that PDF? $\endgroup$
    – Dave
    Jul 26, 2019 at 18:31
  • $\begingroup$ @Xi'an Thank you this helps! $\endgroup$ Jul 26, 2019 at 19:38

If you don't care about the distribution aside from min, max, and mean, then there is a simple answer.

Take 96.476510067114100 percent of draws as 80 and 3.523489932885910 percent of draws as 12000. On average, you get 500, and you have your min and max. I calculated the percentages by solving a system of equations

$$a + b =1$$ $$80a + 12000b = 500$$

The first equation establishes the the values must sum to one, making sure that we are dealing with probabilities. The second equation get us our average of 500.

D <- rep(NA,2000) # define a vector of NAs to hold your sampled values
for (i in 1:2000){
    X <- rbinom(1,1,0.96476510067114100) # determine which value you'll take, 80 or 12000
    if (X==0){D[i] <- 12000} # declare observation i as 12000
    if (X==1){D[i] <- 80} # declare observation i as 80
  • $\begingroup$ Thanks Dave! But I don't want only 80s and 12000s in my distribution. I would like a range of values but all positive $\endgroup$ Jul 26, 2019 at 18:03
  • $\begingroup$ I'm sorry I was not clear earlier $\endgroup$ Jul 26, 2019 at 18:04
  • 2
    $\begingroup$ We can construct a different distribution, but what requirements do you have? $\endgroup$
    – Dave
    Jul 26, 2019 at 18:05
  • $\begingroup$ normal or a poisson but no negative values $\endgroup$ Jul 26, 2019 at 18:07
  • 9
    $\begingroup$ Normal is out, since it can take all real values. More helpful, though, would be to know what you're considering either of those distributions. So...why normal or Poisson? $\endgroup$
    – Dave
    Jul 26, 2019 at 18:11

Use for example a beta distribution, shifted and rescaled to your min and max.

The beta is easy to use here since it is bounded to the interval [0;1], but the mean can be placed by parameterization.

You have mean=alpha/(alpha+beta) and hence beta=alpha/mean - alpha, or in the rescaled version beta=alpha*(max-min)/(mean-min) - alpha. With the parameter alpha you can control the shape, whether you want more values in the extremes or not.

You can also consider a truncated normal distribution. This works quite similar. Again you have to decide for a shape by choosing the standard deviation. This is straight forward to use - fix min, max, mean, and sigma. Compute the resulting mu and you have your data distribution. But the shape of this distribution will look truncated, and not as elegant as a beta distribution.

Beta distributions are smooth. If you want something simpler consider simply using two uniform distributions. Without loss of generality, assume min=0 and max=1 by rescaling and shifting. Split the interval at the (rescaled) mean. Sampling uniformly from [0;mean] with probability p has E[X]=mean/2 and from [mean;1] with 1-p has E[X]=(mean+1)/2. Combining these two and the desired outcome yields p*mean/2+(1-p)(mean+1)/2= mean and solving for p Yields p=1-mean.

Hence a simple strategy is to uniformly sample from [min;mean] with probability 1-(mean-min)/(max-min) and from [mean;max] otherwise. The drawback is the non-smooth (stepwise) CDF.

Ultimately, you could also design the CDF directly. This would be easy if you had fixed the median, but with the mean you'll need to take the values into account. The idea is that you might want to enforce a stepwise linear or polynomial CDF, and choose the function parameters such that the resulting mean is as desired. Please do the math for this yourself.

Last but not least: you are probably asking for a skewed distribution. I would rather fix the median, not the mean. This makes above constructions a lot easier and more meaningful. The mean of a skewed distribution is not too reliable.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.