Skip to content

Choosing a random number between x and y

12 messages · jdeisenberg, Doran, Harold, Rowe, Brian Lee Yung (Portfolio Analytics) +8 more

Vie
#
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
#
Vie wrote:
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
#
See ?runif
-tp21914106p21914106.html
#
How about this:
[1] 0.4806179
[1] 0.1789742


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Vie
Sent: Monday, February 09, 2009 9:41 AM
To: r-help at r-project.org
Subject: [R] Choosing a random number between x and y



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
#
The runif function meets the stated criteria, if that is not good enough, then give us more detail.
#
Vie <wxv579 <at> bham.ac.uk> writes:
assuming you want uniform distribution

?runif 

These functions provide information about the uniform distribution on the
interval from min to max. dunif gives the density, punif gives the distribution
function qunif gives the quantile function and runif generates random deviates.

Dieter
#
Hi Vie,

Something like the following should be fine:

## R Start...
[1] 0.01145260
## R end.

see ?runif for details. Hope that helps a little,
Tony Breyal
On 9 Feb, 14:40, Vie <wxv... at bham.ac.uk> wrote:
#
on 02/09/2009 08:40 AM Vie wrote:
If the numbers are to be uniformly distributed within the range you
specify, see ?runif:
[1] 0.33273548 0.34751295 0.15387123 0.32831769 0.01863003 0.28881336
 [7] 0.25578130 0.47281504 0.29631693 0.21392932
[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
#
If you want to chose numbers from your range with uniform probability

runif(1, 0, 0.5) 

See ?runif

-Christos
#
On Mon, 2009-02-09 at 06:40 -0800, Vie wrote:
Hi Vie 

I don't understand if you need a only random generation or mixture
random generation, so i will make the 3 examples Using runif

1- Random 10 number Retween 0 and 0.5 runif(10,0,0.5)

2 -Random 20 number Retween 0 and 0.7 runif(20,0,0.7)

3- Random 40 number of mixture two random uniforme random 1 and 2 with
p(random1)= 0.3 and p(random=2) = 0.7

ifelse(runif(40)>.3,runif(40,0,0.7),runif(40,0,0.5))