Dear R-help, Which random number generator function would you recommend for simply picking 15 random numbers from the sequence 0-42? I want to use replacement (so that the same number could potentially be picked more than once). I have read the R-help archives and the statistics and computing book on modern Applied statistics with S but the advice seems to be for much form complicated examples, there must be a simpler way for what I am trying to do? If anybody can help me I would greatly appreciate your advice and time, Best Wishes, Jenny ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Jennifer Barnes PhD student: long range drought prediction Climate Extremes Group Department of Space and Climate Physics University College London Holmbury St Mary Dorking, Surrey, RH5 6NT Web: http://climate.mssl.ucl.ac.uk
Assuming you want only integers, see ?sample Sarah On 6/6/07, Jenny Barnes <jmb at mssl.ucl.ac.uk> wrote:> Dear R-help, > > Which random number generator function would you recommend for simply picking 15 > random numbers from the sequence 0-42? I want to use replacement (so that the > same number could potentially be picked more than once). > > I have read the R-help archives and the statistics and computing book on modern > Applied statistics with S but the advice seems to be for much form complicated > examples, there must be a simpler way for what I am trying to do? > > If anybody can help me I would greatly appreciate your advice and time, > > Best Wishes, > > Jenny > >-- Sarah Goslee http://www.functionaldiversity.org
You're all stars - thanks for the replies - I will go ahead and use sample........... I need to do this about 10,000 times - any suggestions for this or simply put it in a loop 10,000 times outputting each time to an array? Best Wishes, Jenny> > > use sample(c(0:42), 15, replace=T) > >hope it helps, >kevin > >----- Original Message ----- >From: Jenny Barnes <jmb at mssl.ucl.ac.uk> >Date: Wednesday, June 6, 2007 10:30 am >Subject: [R] random numbers selection - simple example > >> Dear R-help, >> >> Which random number generator function would you recommend for >> simply picking 15 >> random numbers from the sequence 0-42? I want to use replacement >> (so that the >> same number could potentially be picked more than once). >> >> I have read the R-help archives and the statistics and computing >> book on modern >> Applied statistics with S but the advice seems to be for much form >> complicated >> examples, there must be a simpler way for what I am trying to do? >> >> If anybody can help me I would greatly appreciate your advice and >> time, >> Best Wishes, >> >> Jenny >> >> >> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >> Jennifer Barnes >> PhD student: long range drought prediction >> Climate Extremes Group >> Department of Space and Climate Physics >> University College London >> Holmbury St Mary >> Dorking, Surrey, RH5 6NT >> Web: http://climate.mssl.ucl.ac.uk >> >> ______________________________________________ >> R-help at stat.math.ethz.ch mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting- >> guide.htmland provide commented, minimal, self-contained, >> reproducible code. >>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Jennifer Barnes PhD student: long range drought prediction Climate Extremes Group Department of Space and Climate Physics University College London Holmbury St Mary Dorking, Surrey, RH5 6NT Tel: 01483 204149 Mob: 07916 139187 Web: http://climate.mssl.ucl.ac.uk
On 06-Jun-07 14:30:44, Jenny Barnes wrote:> Dear R-help, > > Which random number generator function would you recommend for > simply picking 15 random numbers from the sequence 0-42? I want > to use replacement (so that the same number could potentially be > picked more than once).R has the function sample() which samples a given number of items from a given set, without replacement by default, but with replacement if you specify this. Enter ?sample for more information. In the above case sample((0:42), 15, replace=TRUE) will do what you seem to describe above. Example:> sample((0:42), 15, replace=TRUE)[1] 26 38 1 41 11 30 22 37 28 0 0 25 10 39 27 if you want them in random order (i.e. "as they come off the line"), or> sort(sample((0:42), 15, replace=TRUE))[1] 1 3 5 8 8 10 16 17 21 25 30 30 33 34 40 if you want them sorted. Best wishes, Ted.> I have read the R-help archives and the statistics and computing > book on modern Applied statistics with S but the advice seems to > be for much form complicated examples, there must be a simpler way > for what I am trying to do? > > If anybody can help me I would greatly appreciate your advice and time, > > Best Wishes, > > Jenny-------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 06-Jun-07 Time: 16:24:15 ------------------------------ XFMail ------------------------------
Seemingly Similar Threads
- loop is going to take 26 hours - needs to be quicker!
- how do you interpolate a gaussian grid to a standard 2.5 degree grid?
- upside down image/data
- Overlaying a single contour from a new data array in levelplot
- problem with loop to put data into array with missing data forsome files