Adrian Johnson
2016-Aug-02 18:56 UTC
[R] generate a vector of random numbers within confines of certain parameters
Dear group, I am trying to generate a vector of random numbers for 20K observation. however, I want to generate numbers (with 6 decimal places) within the range of Std. Dev : 2-3 mean : 4-6 Is there a method to generate numbers with 6 decimal places under these parameters thank you. Adrian
David L Carlson
2016-Aug-02 19:39 UTC
[R] generate a vector of random numbers within confines of certain parameters
Try> set.seed(42) > x1 <- round(rnorm(20000, 4, 2), 6) > head(x1)[1] 6.741917 2.870604 4.726257 5.265725 4.808537 3.787751 Setting the seed makes the sequence reproducible, but if that is not important, you can leave it out. This assumes you want a normal distribution and you want to specify the mean and standard deviation. If you want those to be selected randomly. The following does that using uniform distributions so each mean and standard deviation is equally likely within the range:> x2 <- round(rnorm(20000, runif(1, 4, 6), runif(1, 2, 3)), 6) > head(x2)[1] 4.054289 4.745569 5.795536 6.316750 4.370713 5.586646 ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Adrian Johnson Sent: Tuesday, August 2, 2016 1:57 PM To: r-help Subject: [R] generate a vector of random numbers within confines of certain parameters Dear group, I am trying to generate a vector of random numbers for 20K observation. however, I want to generate numbers (with 6 decimal places) within the range of Std. Dev : 2-3 mean : 4-6 Is there a method to generate numbers with 6 decimal places under these parameters thank you. Adrian ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
David L Carlson
2016-Aug-02 19:48 UTC
[R] generate a vector of random numbers within confines of certain parameters
The second one (x2) uses the same mean/sd for the 20000 variates. If you want to change the mean/sd for each variate:> x3 <- round(replicate(20000, rnorm(1, runif(1, 4, 6), runif(1, 2, 3))), 6) > head(x3)[1] 5.648530 5.689563 2.945512 3.915723 8.527447 0.298535 ------- David C -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of David L Carlson Sent: Tuesday, August 2, 2016 2:40 PM To: Adrian Johnson; r-help Subject: Re: [R] generate a vector of random numbers within confines of certain parameters Try> set.seed(42) > x1 <- round(rnorm(20000, 4, 2), 6) > head(x1)[1] 6.741917 2.870604 4.726257 5.265725 4.808537 3.787751 Setting the seed makes the sequence reproducible, but if that is not important, you can leave it out. This assumes you want a normal distribution and you want to specify the mean and standard deviation. If you want those to be selected randomly. The following does that using uniform distributions so each mean and standard deviation is equally likely within the range:> x2 <- round(rnorm(20000, runif(1, 4, 6), runif(1, 2, 3)), 6) > head(x2)[1] 4.054289 4.745569 5.795536 6.316750 4.370713 5.586646 ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Adrian Johnson Sent: Tuesday, August 2, 2016 1:57 PM To: r-help Subject: [R] generate a vector of random numbers within confines of certain parameters Dear group, I am trying to generate a vector of random numbers for 20K observation. however, I want to generate numbers (with 6 decimal places) within the range of Std. Dev : 2-3 mean : 4-6 Is there a method to generate numbers with 6 decimal places under these parameters thank you. Adrian ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Jeff Newmiller
2016-Aug-02 20:08 UTC
[R] generate a vector of random numbers within confines of certain parameters
x <- rnorm( 20000, 5, 2.5 ) The requirement for "random" is ill-specified because it omits mention of which random distribution you want (I assumed normal distribution above). The requirement for "decimal places" is ill-defined because floating point numbers are internally represented with mantissa and exponent ("scientific notation"), so large numbers have fewer significant "decimal places" in the fraction than small numbers do. For most purposes double precision IEEE754 numbers have more precision than you will need. What gets sticky is if you want to LIMIT the number of decimals... you may need to use the sprintf function and export the data as character values if that is important (which I doubt). Note that the default behavior of the R console is to PRINT values with four decimals, but the rest of the significant digits are really still there. -- Sent from my phone. Please excuse my brevity. On August 2, 2016 11:56:35 AM PDT, Adrian Johnson <oriolebaltimore at gmail.com> wrote:>Dear group, > >I am trying to generate a vector of random numbers for 20K observation. > >however, I want to generate numbers (with 6 decimal places) within the >range of >Std. Dev : 2-3 >mean : 4-6 > >Is there a method to generate numbers with 6 decimal places under >these parameters > >thank you. >Adrian > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
Bert Gunter
2016-Aug-02 21:01 UTC
[R] generate a vector of random numbers within confines of certain parameters
All floating point operations are done to machine precision -- roughly 16 digits. See ?.Machine . You can choose to round, truncate, or display to anything less than that that you care to. See also the digits parameter of ?options The rest of your post is ambiguous to me. But note that (all?/most?) rng's are vectorized, so e.g.> set.seed(1123)> rnorm(10,mean= runif(10,2,3), sd = runif(10,4,6))[1] 4.369411 1.944876 3.143913 6.489048 -1.093468 1.330675 -3.936239 11.740755 [9] -2.260413 -1.748759 ... if that's what you meant. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Tue, Aug 2, 2016 at 11:56 AM, Adrian Johnson <oriolebaltimore at gmail.com> wrote:> Dear group, > > I am trying to generate a vector of random numbers for 20K observation. > > however, I want to generate numbers (with 6 decimal places) within the range of > Std. Dev : 2-3 > mean : 4-6 > > Is there a method to generate numbers with 6 decimal places under > these parameters > > thank you. > Adrian > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.