# Simple user-defined function in R

#### stoptrying

##### New Member
Hi, i was looking through the forum but unfortunately couldn't find solution for my problem.
I need to use simulation to show that sums of independent uniform random variables approach a Gaussian distribution. I want to write function of m, n, a, b , where m is number of simulations, n is size of simulation and a and b are parameters, which will give a sum as an output.
My code is:
fun=function(m,n,a,b){
x=rep(0,n)
sum=rep(0,n)
for (i in (1:m)){
x=runif(n,a,b)
sum=sum+x
}
sum
}

I get warning message that
1: In x = runif(n, a, b) :
number of items to replace is not a multiple of replacement length
which I cannot interpret.
Thanks for your time and help!

#### vinux

##### Dark Knight
Try this
Code:
fun=function(m,n,a,b){
x=rep(0,n)
sum=rep(0,n)
for (i in (1:m)){
x=runif(n,a,b)
sum=sum+x
}
sum
}

if you want x for each i, you need to define x as a matrix.

#### Dason

##### Ambassador to the humans
x=runif(n,a,b)

As vinux pointed out this is your problem. You're saying "give me a vector of size n. Ok now stuff it into a single spot in a different vector". R is telling you that that doesn't make sense.