[R] Generate random numbers up to one

Petr Klasterecky klaster at karlin.mff.cuni.cz
Tue Mar 6 15:43:18 CET 2007



Barry Rowlingson napsal(a):
> Petr Klasterecky wrote:
> 
>> You need to specify what 'random' means. If you have any numbers, you 
>> can always make them add-up to 1:
>> x <- rnorm(100) #runif(100), rpois(100) etc.
>> x <- x/sum(x)
>> sum(x)
> 
>  I see a slight problem that may occur with dividing by sum(x) in 
> certain cases....
> 
> Barry
> 

OK, dividing by 0 is not nice, but the original question was very 
general and I wanted to give some minimal advice at least. However, I 
see a more serious issue I forgot to mention. So just to make it clear: 
sum(x) is a random variable as well and dividing by sum(x) does not 
preserve the original distribution data were generated from.

Petr
-- 
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic



More information about the R-help mailing list