MATLAB Answers

# How to make 3 random numbers that sum to 1

198 views (last 30 days)
Samual on 29 Sep 2017
Commented: CLOUDY LU on 8 Mar 2020
I'm trying to make two matrices for 6 random numbers, and the three different numbers of each need to sum up to 1. ex)P1+P2+P3=1 . How can I make this?? thanks. this is some part of my script file.
P1=rand(1,1)
P2=rand(1,1)
P3=rand(1,1)
Q1=rand(1,1)
Q2=rand(1,1)
Q3=rand(1,1)
if (P1+P2+P3==1) && (Q1+Q2+Q3==1)
%setting P, Q matrix
P=[P1 P2 P3];
Q=[Q1; Q2; Q3];

#### 0 Comments

Sign in to comment.

### Answers (2)

Image Analyst on 29 Sep 2017
Did you try this:
r = rand(1, 3); % Start with 3 random numbers that don't sum to 1.
r = r / sum(r) % Normalize so the sum is 1.
theSum = sum(r) % Check to make sure. Should be 1

#### 3 Comments

Shuva Paul on 5 Mar 2018
Helpful! Thanks :) :)
krishna vasishta on 12 Apr 2018
It works!! Many thanks!!!
CLOUDY LU on 8 Mar 2020
Wonderful idea! Thanks!

Sign in to comment.

Roger Stafford on 5 Mar 2018
Edited: Stephen Cobeldick on 5 Mar 2018
If you want your results statistically uniform area-wise, you can use the 'randfixedsum' function in the File Exchange at:

#### 0 Comments

Sign in to comment.