Generate a random vector. It will sum up to S
. Divide all elements by S
- it now sums up to 1
.
x = rand(1, 100)
x /= sum(x)
sum(x)
# ==> 1
EDIT: added code, renamed the confusing variable.
Question
I want to generate a random vector such that the elements add up to 1. Is that possible in Octave?
I have a bunch of mutually exclusive events (say 100) and I want to assign random probabilities (non-zero and hopefully with a normal distribution) to them such that their probabilities sum up to 1. Is there a way to do that in Octave?
I know of rand(1,100)
but I do not know if it is possible to make sure that the values sum up to a constant.
Solution
Generate a random vector. It will sum up to S
. Divide all elements by S
- it now sums up to 1
.
x = rand(1, 100)
x /= sum(x)
sum(x)
# ==> 1
EDIT: added code, renamed the confusing variable.