# [R] Using loop for a random vector (Montecarlo method)

mary mary.dama at libero.it
Mon Jan 28 12:06:19 CET 2013

```Hi,

I would like to replicate a sort of Montecarlo experiment:

I have to generate a random variable N(0,1) with 100 observations but I have
to contaminate this values at certain point in order to obtain different
vectors with different samples:

tab<-function(N,n,E,L){
for(i in 1:100){
X1<-rnorm(N*(1-a),0,1)
X2<-rnorm(N*(a),E,L)
X_tab<-sample(c(X1,X2),n,replace=T)
}
return(X_tab)}

N=100 ,n=5, a= 0.1, E=5 , L=0.01

I have to replicate it for different values of this parameters, but i
wouldn't rewrite each times values, i would like a loop...
for n=5
N=100
a=c(0.1,0.2,0.3)
E=5
L=0.01

and repeat
for n=5
N=100
a=c(0.1,0.2,0.3)
E=5
L=0.025

for n=5
N=100
a=c(0.1,0.2,0.3)
E=5
L=1

I have to do this for n=5,10,20
and for E=5,10

how to do??
Thanks
M.

--
View this message in context: http://r.789695.n4.nabble.com/Using-loop-for-a-random-vector-Montecarlo-method-tp4656845.html
Sent from the R help mailing list archive at Nabble.com.

```