[R] Choosing a random number between x and y
jdeisenberg
catcode at catcode.com
Mon Feb 9 19:17:14 CET 2009
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'm no R expert, but this should give you n uniformly distributed random
numbers scaled down to the range 0..max where max < 1 (and yes, I know, this
makes it not-so-uniform):
rrange <- function(n, max) { result <- runif(n) * max; result }
Use it as follows:
rrange(12, 0.7) # generate 12 numbers between 0 and 0.7
If you are looking for integer values from a minimum to a maximum
(inclusive), this should work:
irange <- function(n, min,max) { result <- min + trunc(runif(n) * (max -
min + 1)); result }
Used as follows:
irange(12, 5, 20) # generate 12 integers in the range 5..20 inclusive
--
View this message in context: http://www.nabble.com/Choosing-a-random-number-between-x-and-y-tp21914106p21918718.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list