How od i generate random numbers whose sum is constant?

3 views (last 30 days)
I want 100 random numbers bu their sum should be same.
  2 Comments

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 9 Sep 2022
If the numbers are supposed to be >= 0 with precribed sum, a simple method is
n = 100;
s = 10;
Y = -log(rand(1,n));
X = s * Y ./ sum(Y,2)
sum(X)
The justification of log is from the ubiased conditioning probability that I explain here:

More Answers (1)

Bruno Luong
Bruno Luong on 9 Sep 2022
Edited: Bruno Luong on 9 Sep 2022

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!