[R] generate a vector of random numbers within confines of certain parameters

David L Carlson dcarlson at tamu.edu
Tue Aug 2 21:39:40 CEST 2016


> 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.

R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list