Well, I was quite blind not to change 1 to 1000 in runif() and use
replicate()!!
It gets even faster if you create prob first.
Ivan
Le 4/11/2011 10:53, Dennis Murphy a écrit :
Hi:
Let's assume the lengths of each vector are the same so that they can
be multiplied. Here's the timing on my mac
Hi:
Let's assume the lengths of each vector are the same so that they can be
multiplied. Here's the timing on my machine:
> system.time(replicate(1000, { prob<-numeric(1000)
+
+ for (n in 1:1000) {
+ task1 <- runif(1, min=0.8, max= 0.9)
+ task2 <- runif(1, min=0.75, max= 0.85)
+ task3 <- runif(1,
Hi,
I think you can do this without a loop (well, replicate() is based on
sapply()):
prob<-numeric(1000)
task1 <- replicate(1000,runif(1, min=0.8, max= 0.9))
task2 <- replicate(1000,runif(1, min=0.75, max= 0.85))
task3 <- replicate(1000,runif(1, min=0.81, max= 0.89))
prob <- task1*task2*task3
The loop is correct, you just need to make sure that your result is computed
and stored as the n-th element that is returned by the loop. Pick up any
manual of R, and looping will be explained there. Also, I would recommend
that you draw a random number for every iteration of the loop. Defining the
4 matches
Mail list logo