Sample with a max
问题 If I want to sample numbers to create a vector I do: set.seed(123) x <- sample(1:100,200, replace = TRUE) sum(x) # [1] 10228 What if I want to sample 20 random numbers that sum to 100, and then 30 numbers but still sum to 100. This I imagine will be more of a challenge than it seems. ?sample and searching Google has not provided me with a clue. And a loop to sample then reject if not close enough( e.g. within 5) of the desired sum I guess may take some time. Is there a better way to achieve