[R] Choosing a random number between x and y

Marc Schwartz marc_schwartz at comcast.net
Mon Feb 9 20:19:08 CET 2009


on 02/09/2009 08:40 AM Vie wrote:
> Hi,
> 
> Ive been trying to find a function that will allow me to pull out a number
> between a minimum and maximum threshold.
> 
> I want a random decimal number between, for example, 0 and 0.5 or 0 and 0.7.
> I've been searching everywhere for a function that will allow me to do this
> in R, but I have yet to be successful. Any help would be much appreciated.
> 
> Thanks in advance

If the numbers are to be uniformly distributed within the range you
specify, see ?runif:

> runif(10, 0, 0.5)
 [1] 0.33273548 0.34751295 0.15387123 0.32831769 0.01863003 0.28881336
 [7] 0.25578130 0.47281504 0.29631693 0.21392932

> runif(10, 0, 0.7)
 [1] 0.52535461 0.12373738 0.40943192 0.02131712 0.32761085 0.22612708
 [7] 0.40411518 0.33841189 0.02760388 0.03942751

This would be covered, for example, in An Introduction to R:

http://cran.r-project.org/doc/manuals/R-intro.html#Probability-distributions


HTH,

Marc Schwartz




More information about the R-help mailing list