This function generates an n by m array x, each of whose m columns contains n random values lying in the interval [a,b], subject to the condition that their sum be equal to s. The distribution of values is uniform in the sense that it has the conditional probability distribution of a uniform distribution over the whole n-cube, given that the sum of the x's is s.
The function uses the randfixedsum
algorithm, written by Roger Stafford and implemented in MatLab. For details, see http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum/content/randfixedsum.m
RandVec(a=0, b=1, s=1, n=9, m=1, Seed=sample(1:1000, size = 1))
An object of class RandVec
with components,
The randomly generated vectors.
The function RandVec
generates an n
by m
matrix x. Each of the m
columns contain n
random values lying in the interval [a,b]. The argument a
specifies the lower limit of the interval. Default 0
.
The argument b
specifies the upper limit of the interval. Default 1
.
The argument s
specifies the value to which each of the m
generated columns should sum to. Default 1
.
The number of requested elements per column. Default 9
.
The number of requested columns. Default 1
.
The seed that is used. Default sample(1:1000, size = 1)
.
Wim Van der Elst, Ariel Alonso, & Geert Molenberghs
The function is an R adaptation of a matlab program written by Roger Stafford. For details on the original Matlab algorithm, see: http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum/content/randfixedsum.m
# generate two vectors with 10 values ranging between 0 and 1
# where each vector sums to 1
# (uniform distribution over the whole n-cube)
Vectors <- RandVec(a=0, b=1, s=1, n=10, m=2)
sum(Vectors$RandVecOutput[,1])
sum(Vectors$RandVecOutput[,2])
Run the code above in your browser using DataLab