# randomly sample a vector from any distribution (e.g. gamma distribution)

#### Kairos2012

##### New Member
Hi,

help is very much appreciated. Thanks in advance!!!

rgamma(...)
As far as I know with this function (code) one can randomly sample 100 numbers simultanously.

Question: is it possible to sample 100 random numbers e.g. from 100 different gamma distributions using only one command line (or: using only one function)?

One idea how this on command line could look like:
rgamma(1:100, shape=c(s1,s2,...,s100), rate=c(r1,r2,...,r100))

What I do not know: is it correct to assume that effectively only ONE random number (with the respective paramateres s1<>s2<>...<>s100 und r1<>r2<>...<>r100) has been drawn from only ONE gamma distribution (out of 100 different gamma distributions) ?

Thanks again for any thoughts and advice!
Merry Christmas!

#### Dason

If all you want is a single random draw from each of the 100 gamma distributions then all you need to do is change your n to 100 (instead of 1:100).
Code:
# The first is from a gamma(1,1), the second from gamma(100, 1)
rgamma(2, c(1, 100), c(1, 1))
If you wanted to vary the number of observations from each of the gamma distributions then you could use mapply

Code:
# Grab 1 observation from gamma(6, 1,)
# grab 2 from gamma(7, 1)
# grab 3 from gamma(8, 1)
# grab 4 from gamma(9,1)
# grab 5 from gamma(10, 1)
mapply(rgamma, n = 1:5, shape = 6:10, rate = rep(1, 5))