[R] Drawing random numbers from Uniform distribution with infinite range

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Tue Jul 29 02:12:15 CEST 2025


There are an infinite number of values between 0 and 1. Likewise between any two values, though it might get tricky if they were infinitely close together. The real numbers also go from negative infinity to positive infinity. Computers have limited memory that can handle a minute portion of this range.

There is also an issue with random number generator. Most computer random number generators are pseudo-random. That is they are "random" with some range, but expanding beyond that range they show patterns. There are services that will provide true random numbers (e.g. https://www.random.org/), but there may be limits to the rate the numbers are generated or provided to you.

"without preference of any particular probability distribution" but the Uniform distribution is a probability distribution.

All computer numbers are discrete. If the computer can process 17 decimal places, then anything in the twentieth decimal place will be lost. If your computer can handle 30 decimal places, then anything in the fiftieth decimal place will be lost. This results in number gaps between consecutive values at the smallest significant digit. So even a Uniform distribution where every value has equal probability of being chosen is not true for values between consecutive least significant digit.

Significant digits is not decimal places. 12345654678239543 is 17 digits as is. 0. 1234565467823954. However, 12345654678239543.1234565467823954 is 34 digits. Also, 123456546782395430000000 can be a stored number in the computer but if accuracy is 17 digits then the trailing zeros will always be zero (or some random noise). A program like R can handle very large and small numbers, but not every digit will be kept in memory.

Things get more complex in that the computer must store things in binary. Not all real numbers have a finite binary equivalent. This can make a simple program give unexpected outcomes.

options(digits=20)
for (i in 1:100) {
  j <- 1 + 0.1^i
  if (j == 1) {
    cat("At i =", i, "-> 1 + 0.1^i == 1 (difference lost due to precision)\n")
  } else {
    cat("i =", i, "j =", j, "\n")
  }
}

Everything looks good up until options=16. At options=17 the results are different.
There are programming strategies to overcome this limit, but there is still a limit.

library(Rmpfr)
precBits <- 256  # bits of precision (~77 decimal digits)
for (i in 1:50) {
  small <- mpfr("0.1", precBits)^i
  j <- 1 + small
  if (j == 1) {
    cat("At i =", i, "j == 1 (even with high precision)\n")
    break
  } else {
    cat("i =", i, "j =", format(j, digits = 40), "\n")
  }
}

Eventually, you run out of computer memory. If this is a part of a large process the execution time may be long.

Tim



-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Daniel Lobo
Sent: Monday, July 28, 2025 12:30 PM
To: Rui Barradas <ruipbarradas using sapo.pt>
Cc: r-help using r-project.org
Subject: Re: [R] Drawing random numbers from Uniform distribution with infinite range

[External Email]

Many thanks for your guidance. However my original problem is, how to select n points in the Real line randomly without any preference of any particular probability distribution?

On Mon, 28 Jul 2025 at 21:45, Rui Barradas <ruipbarradas using sapo.pt> wrote:
>
> 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 using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://st/
> > at.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C02%7Ctebert%40ufl
> > .edu%7Cef232b26cd0a45cb285f08ddcdf70798%7C0d4da0f84a314d76ace60a6233
> > 1e1b84%7C0%7C0%7C638893183054544956%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0
> > eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIs
> > IldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=IraxT8iu7WasjesdmwC3KXg7qXjxaQJL
> > WER%2FGenW%2BRs%3D&reserved=0 PLEASE do read the posting guide
> > https://ww/
> > w.r-project.org%2Fposting-guide.html&data=05%7C02%7Ctebert%40ufl.edu
> > %7Cef232b26cd0a45cb285f08ddcdf70798%7C0d4da0f84a314d76ace60a62331e1b
> > 84%7C0%7C0%7C638893183054560855%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1h
> > cGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldU
> > IjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=%2FrlPXP%2BCnUQ1S5yBOBaf3zj71vrQYF9m
> > cJ8woJ0QI60%3D&reserved=0 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.w/
> ikipedia.org%2Fwiki%2FContinuous_uniform_distribution&data=05%7C02%7Ct
> ebert%40ufl.edu%7Cef232b26cd0a45cb285f08ddcdf70798%7C0d4da0f84a314d76a
> ce60a62331e1b84%7C0%7C0%7C638893183054570228%7CUnknown%7CTWFpbGZsb3d8e
> yJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWF
> pbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=bimbtz4%2BHqya58n4A1CVpHsWZ4I
> 1hoDq25gY7z81HII%3D&reserved=0

______________________________________________
R-help using 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.



More information about the R-help mailing list