Daniel Lobo
2025-Jul-28 16:00 UTC
[R] Drawing random numbers from Uniform distribution with infinite range
Hi, I want to draw a set of random number from Uniform distribution where Support is the entire Real line. runif(4, min = -Inf, max = Inf) However it produces all NAN Could you please help with the right approach?
peter dalgaard
2025-Jul-28 16:10 UTC
[R] Drawing random numbers from Uniform distribution with infinite range
That makes no sense, mathematically. If a measure has constant density over an infinite region, then the total mass is infinite, i.e. not a probability measure. -pd> On 28 Jul 2025, at 18.00, Daniel Lobo <danielobo9976 at gmail.com> wrote: > > Hi, > > I want to draw a set of random number from Uniform distribution where > Support is the entire Real line. > > runif(4, min = -Inf, max = Inf) > > However it produces all NAN > > Could you please help with the right approach? > > ______________________________________________ > 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 https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business SchoolSolbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Chris Ryan
2025-Jul-28 16:14 UTC
[R] Drawing random numbers from Uniform distribution with infinite range
What would a pdf of such a distribution even look like? Intuitively, it seems like the pdf everywhere would be, for all practical purposes, zero? So no probability of drawing any value? --Chris Ryan Daniel Lobo wrote:> Hi, > > I want to draw a set of random number from Uniform distribution where > Support is the entire Real line. > > runif(4, min = -Inf, max = Inf) > > However it produces all NAN > > Could you please help with the right approach? > > ______________________________________________ > 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 https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Rui Barradas
2025-Jul-28 16:15 UTC
[R] Drawing random numbers from Uniform distribution with infinite range
On 7/28/2025 5:00 PM, Daniel Lobo wrote:> Hi, > > I want to draw a set of random number from Uniform distribution where > Support is the entire Real line. > > runif(4, min = -Inf, max = Inf) > > However it produces all NAN > > Could you please help with the right approach? > > ______________________________________________ > 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 https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.Hello, What you are asking doesn't make sense. The uniform distribution's PDF is f(x;a, b) = 1/abs(b - a) if x in [a, b] 0 otherwise So what you have is 1/abs(Inf - -Inf) = 1/abs(Inf) = 0. And the cumulative distribution function is even worse, it will give you the indeterminate Inf/Inf. See the Wikipedia on the uniform distribution [1]. [1] https://en.wikipedia.org/wiki/Continuous_uniform_distribution
Ben Bolker
2025-Jul-28 16:17 UTC
[R] Drawing random numbers from Uniform distribution with infinite range
Can you please give us more context? R can't represent numbers on the entire real line, and it's difficult to imagine this sample making any sense in any applied context. I would normally think to try: mm <- .Machine$double.xmax runif(4, min = -mm, max = mm) but that doesn't work (it gives all Inf) because internally (see the code at https://github.com/r-devel/r-svn/blob/3bde0ee96c3f97050be6b297c28c9edcf9c93eeb/src/nmath/runif.c#L37 ) R picks a uniform deviation on (0,1) and then scales it by (a+(b-a)*u); while R can handle mm above as a floating point number, it can't handle (mm-(-mm)) = 2*mm runif(4, min = -mm/2, max = mm/2) but even this will work very badly because of floating-point imprecision (see below). Maybe we can help if you can tell us more about your ultimate goal. cheers Ben Bolker set.seed(101); runif(100, min = -mm/2, max = mm/2) [1] -2.297481e+307 -8.200630e+307 3.769475e+307 2.834789e+307 -4.496826e+307 [6] -3.594401e+307 1.525642e+307 -2.993750e+307 2.193401e+307 8.238568e+306 [11] 6.827562e+307 3.718973e+307 4.170155e+307 7.759462e+307 -8.067940e+306 [16] 1.623672e+307 5.760458e+307 -4.959503e+307 -1.587959e+307 -8.294366e+307 [21] 3.608178e+307 8.212536e+307 -5.153051e+307 2.895392e+307 7.609974e+307 [26] 5.316134e+307 -7.708282e+307 -1.988109e+307 -1.681720e+307 2.864715e+307 [31] -1.377983e+307 -3.218150e+307 -5.433874e+307 -6.055168e+307 4.190616e+306 [36] 7.433077e+307 -5.271327e+307 5.649844e+307 -8.625921e+307 7.636680e+307 [41] -1.169188e+307 -1.041679e+307 4.686396e+307 -3.002843e+307 -1.899134e+307 [46] -4.794550e+307 -7.701180e+307 7.431367e+307 4.884217e+307 -7.055287e+307 [51] -7.564472e+307 -1.188465e+307 3.231789e+307 4.213006e+307 -8.537088e+306 [56] 5.101596e+307 3.232891e+307 3.362343e+306 3.430102e+307 1.586925e+307 [61] 5.655357e+307 5.573657e+307 1.976310e+307 8.869991e+307 6.170412e+307 [66] 3.874090e+307 -8.645444e+307 -3.504059e+307 6.884230e+307 7.934061e+307 [71] -4.774349e+307 7.849912e+307 1.199097e+307 6.162320e+307 5.776009e+307 [76] -3.955413e+307 -8.138136e+307 -4.944723e+307 3.111675e+307 8.250169e+307 [81] 3.330406e+307 4.958091e+307 4.958730e+307 8.679169e+307 -8.805305e+307 [86] 8.136105e+307 -3.183070e+307 -1.294243e+307 -6.570628e+307 -8.658979e+307 [91] 2.813381e+307 7.455633e+307 3.870431e+307 -5.699560e+307 -4.672777e+307 [96] 6.048322e+307 -2.051645e+307 -4.807502e+307 -7.749484e+307 -7.869860e+307 ) On 7/28/25 12:00, Daniel Lobo wrote:> Hi, > > I want to draw a set of random number from Uniform distribution where > Support is the entire Real line. > > runif(4, min = -Inf, max = Inf) > > However it produces all NAN > > Could you please help with the right approach? > > ______________________________________________ > 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 https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Dr. Benjamin Bolker Professor, Mathematics & Statistics and Biology, McMaster University Director, School of Computational Science and Engineering * E-mail is sent at my convenience; I don't expect replies outside of working hours.
Richard O'Keefe
2025-Jul-29 20:44 UTC
[R] Drawing random numbers from Uniform distribution with infinite range
The uniform distribution with infinite range gives each possible answer infinitesimal probability of being chosen. In the standard real numbers, that probability is ZERO. In fact, the probability of getting a number in *any* prespecified finite range is the same, ZERO. One such finite range is the numbers that can be represented in floating-point. So you are with certainty not going to draw a representable number. A complete answer to your question is thus ifelse(runif(N) < 0.5, 1, -1)/0 Why do you think you need this? On Tue, 29 Jul 2025 at 04:01, Daniel Lobo <danielobo9976 at gmail.com> wrote:> > Hi, > > I want to draw a set of random number from Uniform distribution where > Support is the entire Real line. > > runif(4, min = -Inf, max = Inf) > > However it produces all NAN > > Could you please help with the right approach? > > ______________________________________________ > 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 https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Richard O'Keefe
2025-Jul-30 12:11 UTC
[R] Drawing random numbers from Uniform distribution with infinite range
Let's look at something that *would* work if it were not that IEEE doubles are relatively small discrete set,. Suppose we had two things. - a U(0,1) uniform random generator able to generate any *real* in the range 0 .. 1 - an implementation of atanh() that works for any real in the range 0 .. 1 and can return any real number. Then atanh(runif(n)*2 - 1) would do pretty much what you want,. Try it in R. f <- function (n = 1000000) atanh(runif(n)*2 - 1) summary(f()) It turns out that working with *representable* numbers means that the results of f() are limited to roughly -18,.4 to 18.4, and with n = 1000000 the extremes are almost always around 7. Something that, for actual real numbers, could return *any* real, for representable numbers can only return -18-and-a-bit to +18-and-a-bit. This suggests a completely different approach to your original problem, whatever it is. Instead of working with the entire real line, transform your problem to work with the interval (0,1). On Tue, 29 Jul 2025 at 04:01, Daniel Lobo <danielobo9976 at gmail.com> wrote:> > Hi, > > I want to draw a set of random number from Uniform distribution where > Support is the entire Real line. > > runif(4, min = -Inf, max = Inf) > > However it produces all NAN > > Could you please help with the right approach? > > ______________________________________________ > 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 https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
peter dalgaard
2025-Aug-05 10:02 UTC
[R] Drawing random numbers from Uniform distribution with infinite range
Or rlogis(1e6, scale=.5) which is the same thing. But the logistic distribution is in no reasonable sense a uniform on the entire line, any more than the Gaussian is. (Some notion of a "random real" is involved in Benford's law of first digits, but that involves having a uniform distribution of log(X) and then increasing the range.) -pd> On 30 Jul 2025, at 14:11 , Richard O'Keefe <raoknz at gmail.com> wrote: > > Let's look at something that *would* work if it were not that IEEE > doubles are relatively small discrete set,. > > Suppose we had two things. > - a U(0,1) uniform random generator able to generate any *real* in the > range 0 .. 1 > - an implementation of atanh() that works for any real in the range 0 > .. 1 and can return any real number. > Then atanh(runif(n)*2 - 1) would do pretty much what you want,. > Try it in R. > f <- function (n = 1000000) atanh(runif(n)*2 - 1) > summary(f()) > It turns out that working with *representable* numbers means that the > results of f() are limited to > roughly -18,.4 to 18.4, and with n = 1000000 the extremes are almost > always around 7. > Something that, for actual real numbers, could return *any* real, for > representable numbers > can only return -18-and-a-bit to +18-and-a-bit. > > This suggests a completely different approach to your original > problem, whatever it is. > Instead of working with the entire real line, transform your problem > to work with the interval (0,1). > > On Tue, 29 Jul 2025 at 04:01, Daniel Lobo <danielobo9976 at gmail.com> wrote: >> >> Hi, >> >> I want to draw a set of random number from Uniform distribution where >> Support is the entire Real line. >> >> runif(4, min = -Inf, max = Inf) >> >> However it produces all NAN >> >> Could you please help with the right approach? >> >> ______________________________________________ >> 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 https://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > 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 https://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com