# [R] random numbers with constraints

Ralf Goertz r_goertz @end|ng |rom web@de
Wed Jan 27 10:50:14 CET 2021

```Am Wed, 27 Jan 2021 09:03:15 +0100
schrieb Denis Francisci <denis.francisci using gmail.com>:

> Hi,
> I would like to generate random numbers in R with some constraints:
> - my vector of numbers must contain 410 values;
> - min value must be 9.6 and max value must be 11.6;
> - sum of vector's values must be 4200.
> Is there a way to do this in R?
> And is it possible to generate this series in such a way that it
> follows a specific distribution form (for example exponential)?
> Thank you in advance,

In principle it should be possible. But I guess you are asking too much
with three given values considering that you only have one paramter for
the exponential distribution. For instance, if you only had given min
and max, and wanted a normal distribution then you could have just taken
410 random values from a standard normal: x=rnorm(410) then centered it:
x=x-mean(x) then scaled it so its span equals the one for your given max
(M) and min (m) values: x=x*(M-m)/(max(x)-min(x)) and finally shift it
such that the mininum becomes m: x=x-min(x)+m. Note however, that the
things you are allowed to do with your vector of random numbers depend
on the distribution if you want the result to still follow that type of
distribution.

```