I would like to generate N random integers that sum to M and each integer must be randomly chosen from a uniform distribution.
My first idea was:
1) To enumerate different partitions of M, then
2) To randomly permute the elements of a vector
Here is my code to do this:
library(partitions)
library(gtools)
N <- 10
M <- 5
x <- t(restrictedparts(M, N))
x <- split(x, seq(nrow(x)))
xp <- permute(x[[sample(1:length(x), 1)]])
However, I am not sure that my idea is correct because each integer is not randomly chosen from a uniform distribution and the code is too long when M >= 100. Is there a way to do this?
Related questions in Python