How about
Ntrials <- 100
trials <- sapply(1:Ntrials, function(trialnumber) sum(runif(1000,0,1) < .01)
)
This is pretty quick on my machine, about 15 seconds for Ntrials = 10000.
The tapply method which creates a large matrix of all the uniforms first
may be rather memory intensive; this probably depends on your individual
machine setup though.
Regards, Andrew
-------------------------------
Andrew Sinclair, Research Group
ED&F Man Investment Products
Sugar Quay, Lower Thames St
London, EC3R 6DU
Tel : 020 72852089
Email : asinclair@edfman.co.uk
>-----Original Message-----
>From: Daikwon Han [mailto:dhan@acsu.buffalo.edu]
>Sent: Friday, November 17, 2000 1:01 AM
>To: s-news@lists.biostat.wustl.edu
>Subject: [S] simulation
>
>
>
>Dear all,
>
> Can anyone help me to create 100 numbers from the below program ?
>I'm doing simulation to pick up random numbers and calculate
>the change of
>total values over time. The first thing I did is to pick up the 1000
>numbers between 0 and 1 under the uniform distribution and add all the
>values less than .01 as follows.
>
> x<-runif(1000,0,1)
> x<-sum(as.numeric(x<.01))
>
> Let's say, I got 9 as x value at the first trial, then I want
>to repeat
>it 100 times so that I can get 100 different numbers.
> I've tried "for(i in 1:100)" and "while(i<101)" loop, but it
>doesn't work
>well. I also tried matrix, append command, I only got the same number
>repeated 100 times, probably the first one(same 100 numbers in
>the table).
>
> My question is how to make a loop to keep each value at each
>trial(should
>be 100 different numbers at 100 trials). Any help would be appreciated.
> Thank you.
>
>-----------------------------
> Daikwon Han, Ph.D Candidate
> Department of Geography
> SUNY at Buffalo
> Buffalo, NY 14226
>-----------------------------
>
>
>
>
>
>
>
>---------------------------------------------------------------------
>This message was distributed by s-news@lists.biostat.wustl.edu. To
>unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
>the BODY of the message: unsubscribe s-news
>
**********************************************************************
Any information in this communication which is confidential must not
be disclosed to others without our consent. Such consent is not required
where the information is publicly available and intended for onward
distribution. If the information is confidential and if you are not the
intended recipient, you are not authorised to and must not disclose,
copy, distribute, or retain this message or any part of it. You are
requested to return this message to the sender immediately.
Due to the electronic nature of e-mail, there is a risk that the
information contained in this message has been modified.
Consequently E D & F Man can accept no responsibility or
liability as to the completeness or accuracy of the information.
**********************************************************************
|