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 -- View this message in context: http://www.nabble.com/Choosing-a-random-number-between-x-and-y-tp21914106p21914106.html Sent from the R help mailing list archive at Nabble.com.
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.
See ?runif> -----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 > > > -- > View this message in context: > http://www.nabble.com/Choosing-a-random-number-between-x-and-y-tp21914106p21914106.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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. >
Rowe, Brian Lee Yung (Portfolio Analytics)
2009-Feb-09 19:14 UTC
[R] Choosing a random number between x and y
How about this:> runif(1,0,0.5)[1] 0.4806179> runif(1,0,0.7)[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 -- View this message in context: http://www.nabble.com/Choosing-a-random-number-between-x-and-y-tp2191410 6p21914106.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list 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. -------------------------------------------------------------------------- This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. References to "Merrill Lynch" are references to any company in the Merrill Lynch & Co., Inc. group of companies, which are wholly-owned by Bank of America Corporation. Securities and Insurance Products: * Are Not FDIC Insured * Are Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a Condition to Any Banking Service or Activity * Are Not Insured by Any Federal Government Agency. Attachments that are part of this E-communication may have additional important disclosures and disclaimers, which you should read. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing. --------------------------------------------------------------------------
The runif function meets the stated criteria, if that is not good enough, then give us more detail. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----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 7: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 > > > -- > View this message in context: http://www.nabble.com/Choosing-a-random- > number-between-x-and-y-tp21914106p21914106.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Vie <wxv579 <at> bham.ac.uk> writes:> > 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.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...> n<-1 > my.min <- 0 > my.max <- 0.7 > runif(n, my.min, my.max)[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:> 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 > > -- > View this message in context:http://www.nabble.com/Choosing-a-random-number-between-x-and-y-tp2191... > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
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 advanceIf 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
Hi Vie, Take a look at ?runif. set.seed(123)> runif(1,0,0.5)[1] 0.1437888> runif(1,0,0.7)[1] 0.5518136 HTH, Jorge On Mon, Feb 9, 2009 at 9:40 AM, Vie <wxv579@bham.ac.uk> 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 > > > -- > View this message in context: > http://www.nabble.com/Choosing-a-random-number-between-x-and-y-tp21914106p21914106.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > 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. >[[alternative HTML version deleted]]
If you want to chose numbers from your range with uniform probability runif(1, 0, 0.5) See ?runif -Christos> -----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 > > > -- > View this message in context: > http://www.nabble.com/Choosing-a-random-number-between-x-and-y > -tp21914106p21914106.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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. > >
On Mon, 2009-02-09 at 06:40 -0800, 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 advanceHi 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)) -- Bernardo Rangel Tura, M.D,MPH,Ph.D National Institute of Cardiology Brazil
Thanks a lot everyone. You've been a great help! -- View this message in context: http://www.nabble.com/Choosing-a-random-number-between-x-and-y-tp21914106p21930844.html Sent from the R help mailing list archive at Nabble.com.