[R] Random Variable Generation

David L Carlson dcarlson at tamu.edu
Mon Sep 25 18:01:08 CEST 2017


Once you have assigned the variable, it does not change unless you change it. If you want to draw random numbers repeatedly, put the code into a function:

rnd <- function() {.35 * (runif(1, min=.81, max=1.03) + runif(1, min=1.06, max=1.17) + runif(1, min=-.26, max=1.38)) + 
   .30 * (runif(1, min=.36, max=.98) + runif(1, min=.76, max=.93) + runif(1, min=.52, max=1.03)) + 
   .20 * (runif(1, min=.36, max=.75) + runif(1, min=.1, max=.67) + runif(1, min=.9, max=.96)) + 
   .15 * (runif(1, min=.03, max=.20) + runif(1, min=.27, max=.51) + runif(1, min=.49, max=.55)) + 
   .40 * (runif(1, min=.33, max=.39) + runif(1, min=.84, max=1.35)) + 
   .35 * (runif(1, min=.16, max=.58) + runif(1, min=.05, max=1.29)) + 
   .25 * (runif(1, min=.21, max=.52) + runif(1, min=.20, max=.68)) + 
   .50 * (runif(1, min=1.26, max=1.38))
}

Now every time you access the function, you will get a new random number:

> rnd()
[1] 4.036111
> rnd()
[1] 3.88048
> rnd()
[1] 3.984268
> rnd()
[1] 3.808441
> rnd()
[1] 4.219925

----------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77843-4352



-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Grant Beanblossom
Sent: Monday, September 25, 2017 9:21 AM
To: r-help at r-project.org
Subject: [R] Random Variable Generation

I am attempting to write a code that will generate a win probability for a hockey team. To do this, I have a code built that will generate a number of random variables between two standard deviations, and then weight them and add them together. However, when I attempt to assign this code to a variable, any time I use the variable, it will always give me the same numbers instead of being random every time. Is there a way I can do this so that it is random every time?


Here is the code I am currently using: .35*(runif(1, min=.81, max=1.03)+runif(1, min=1.06, max=1.17)+runif(1, min=-.26, max=1.38))+.3*(runif(1, min=.36, max=.98)+runif(1, min=.76, max=.93)+runif(1, min=.52, max=1.03))+.2*(runif(1, min=.36, max=.75)+runif(1, min=.1, max=.67)+runif(1, min=.9, max=.96))+.15*(runif(1, min=.03, max=.2)+runif(1, min=.27, max=.51)+runif(1, min=.49, max=.55))+.4*(runif(1, min=.33, max=.39)+runif(1, min=.84, max=1.35))+.35*(runif(1, min=.16, max=.58)+runif(1, min=.05, max=1.29))+.25*(runif(1, min=.21, max=.52)+runif(1, min=.20, max=.68))+.5*(runif(1, min=1.26, max=1.38)).


The reason I want to assign it to a variable is because I want to run this code 1000 times and then get a mean value for it, which I believe might be easier if there's a variable assigned to it. However, if there is an easier way to do that, that could work as well.


	[[alternative HTML version deleted]]

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



More information about the R-help mailing list