Basically I want to make a function that makes an n length vector of random proportions that sum to 1 (100%). I can do it if I know n and fix the function at at certain length of the vector but not if I allow n to be free. I'd really like to use an apply family solution (or non loop if there's some solution I can't for see using neither loop or apply) but if that's not possible a loop is fin (I'd actually like to see both as it'll help with the thinking):

**A forced n (works but not what I want**

Code:

```
p <- function(){
v <- sample(seq(0, 1, by=.01), 1)
w <- sample(seq(0, 1-v, by=.01), 1)
x <- sample(seq(0, 1-(v + w), by=.01), 1)
y <- sample(seq(0, 1-(v + w + x), by=.01), 1)
z <- round(1-(v + w + x + y), 2)
c(v, w, x, y, z)
}
p()
```

**an attempt to use global assignment to generate the function**

*I'm actually not sure why this approach doesn't work*

Code:

```
n<-4
y <- 0
sapply(seq_len(n), function(i) {
x <- sample(seq(0, 1-y, by=.01), 1)
y <<- y + x
}
)
```

**What I'd like to get:**

Code:

```
p(n=4)
[1] 0.24 0.01 0.50 0.25
p(n=4)
[1] 0.16 0.05 0.70 0.09
p(n=5)
[1] 0.30 0.49 0.15 0.01 0.05
```