Re-scaling of exponentially distributed random numbers

I am trying to generate $$M$$ random numbers which are exponentially distributed and whose sum adds up to $$N$$ (for simplicity, $$N=1$$).

I found that the generated numbers are initially exponentially distributed. However, after re-scaling they become uniformly distributed. What is the reason for that? And is there a solution?

Here is the result: Any suggestions would be greatly appreciated.

P.S. My code written in Matlab:

subplot(121)
samples = 10000;
lambda = 1;
X = -log(rand(samples,2))/lambda;
hist(X(:,1),100)
subplot(122)
X = X./sum(X,2); % re-scaling
hist(X(:,1),100)
• Note that sum(X,2) computes the sum of rows. – user226604 May 25 at 15:36
• There is no such thing as "random numbers which are exponentially distributed and whose sum adds up to N:" an exponential distribution assigns some probability to arbitrarily large numbers, whereas limiting the sum to $N$ eliminates that possibility. Could you clarify what you actually need to accomplish? – whuber May 25 at 21:23

[Answer revised in view of helpful comment from @whuber.]

If you know the exponential rate $$\theta,$$ then dividing by $$M\theta$$ will give you a total near $$N=1.$$ If you don't know $$\theta$$ and $$M$$ is sufficiently large that the $$\theta$$ is well approximated by the reciprocal of the sample mean (a random variable), then you can still come close.

In what follows, I assume $$\theta$$ is unknown and $$M$$ is large. Then I adjust by dividing by the sum.

In R:

set.seed(525)
x = rexp(10000);  y = x/sum(x)
sum(y)
 1

hist(y, prob=T, ylim=c(0, 10000), col="skyblue2")
curve(dexp(x, 1/mean(y)), add=T, col="red", lwd=2, n = 10001) Even with smaller $$M = 100,$$ the adjusted sample has sum $$1$$ and is nearly exponential.

set.seed(1234)
x = rexp(100);  y = x/sum(x)
sum(y)
 1
sum(x)
 97.64598

ks.test(y, "pexp", 100)

One-sample Kolmogorov-Smirnov test

data:  y
D = 0.084865, p-value = 0.4674
alternative hypothesis: two-sided

The binning is slightly inconsistent between the two histograms below because $$Y = X/97.646,$$ not $$X/100.$$ • This answer is an illusion owing to the large sample size. Try it with, say, a sample of 3 rather than 10,000. You will have to repeat your experiment a few times, but it will quickly become apparent that the distribution you create is far from exponential. – whuber May 25 at 21:22
• Sorry and thanks. Somehow I was focused on approximate results and M large. Accordingly, I made changes to my Answer. – BruceET May 25 at 23:07