Generate 3 random number that sum to 1 in R

前端 未结 6 1164
花落未央
花落未央 2020-12-16 18:15

I am hoping to create 3 (non-negative) quasi-random numbers that sum to one, and repeat over and over.

Basically I am trying to partition something into three rand

6条回答
  •  遥遥无期
    2020-12-16 19:03

    I would simply randomly select 3 numbers from uniform distribution and then divide by their sum. Code as below.

    n <- 3
    x <- runif(3, 0, 1)
    y <- x/sum(x)
    sum(y)== 1
    

    n could be any number you like.

提交回复
热议问题