try this:

> x <- matrix(runif(100), ncol = 4)
> # now scale each row to add up to 100
> x.new <- t(apply(x, 1, function(.row) .row * 100 / sum(.row)))
>
>
> x.new
           [,1]       [,2]      [,3]       [,4]
 [1,] 25.868978 31.4867075 34.547444  8.0968705
 [2,] 35.803781 28.3485728 27.177462  8.6701847
 [3,] 25.643189 30.3149241  7.483549 36.5583374
 [4,] 38.446712  0.5365802 20.995852 40.0208556
 [5,] 47.536877  8.2794082 38.856246  5.3274690
 [6,] 10.823394 26.9903105 38.412020 23.7742755
 [7,] 39.918853  8.6918958  4.832078 46.5571727
 [8,] 41.247319 15.2750550 14.455257 29.0223693
 [9,]  9.884125 26.4028579 32.209590 31.5034270
[10,] 35.204957 43.8716354 13.924313  6.9990947
[11,] 34.924286 26.5681074 26.326393 12.1812132
[12,] 18.912973 24.9559474 20.356519 35.7745604
[13,]  8.153906 33.5810684 29.775884 28.4891416
[14,] 27.413875 21.9299141 25.259566 25.3966449
[15,] 11.269190 28.9627559 29.075971 30.6920828
[16,] 18.811165 34.8912360 28.570353 17.7272457
[17,] 39.415980  0.5446147 28.924644 31.1147609
[18,] 24.307223 26.2213879 20.574576 28.8968129
[19,] 46.665131 27.2148990  6.254606 19.8653638
[20,]  8.840080 45.5712404 33.149987 12.4386918
[21,] 12.096195  8.5512196 49.564027 29.7885583
[22,] 32.808282 33.2820102 33.730519  0.1791886
[23,]  5.171651 46.1836607  6.718998 41.9256910
[24,]  9.395720  3.6877559 39.528280 47.3882438
[25,]  2.061780 14.1513358 43.848042 39.9388425
>

On Mon, Jan 9, 2012 at 10:36 AM, Duncan Murdoch
<murdoch.dun...@gmail.com> wrote:
> On 09/01/2012 10:27 AM, David Winsemius wrote:
>>
>> On Jan 9, 2012, at 10:06 AM, arunkumar1111 wrote:
>>
>> >  Hi
>> >  I want to generate 4 random number which sum up to 100 always
>>
>> "Random" plus condition == "non-random"
>>
>> Perhaps you want 3 "random" numbers conjoined to the difference of
>> their sum and 100? Or perhaps you want 4 "random" numbers multiplied
>> by 100/sum(.)
>>
>> (In either case you will not be getting "4 random numbers".)
>
>
> There's quite a lot of ambiguity in the word "random".  For example, you
> seem to be assuming a "random number"  has to come from a uniform
> distribution, but I would say any distribution except the trivial point mass
> on one point is "random".
>
> So an answer to the original query in my usage is the 4-tuple (a,b,c,d):
>
> a <- 0
> b <- 0
> c <- runif(1)
> d <- 100-c
>
> but this is probably not what was wanted.
>
> Duncan Murdoch
>
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to