To sample uniformly from -A to A is impossible in R, because R only
deals with a tiny subset of those numbers when A > 0.
However, as computational statisticians, we're all quite used to
pretending that runif(n, -1, 1) samples uniformly from -1 to 1, even
though it gives a sample on a discrete subset of the interval. For most
purposes that's a good enough approximation.
So if you want to sample from -A to A, just multiply the result above by A:
A * runif(n, -1, 1)
This produces a pretty good approximation to the required result even
when A == Inf. You'll only get 3 values: -Inf, NaN, Inf (and might not
see any NaN values, which arise when runif(n, -1, 1) gives an exact 0).
I say that's a good approximation, because if you want samples uniformly
distributed on -A to A, you should only get values that would be
represented by +/- Inf in R in the limit as A -> Inf.
Duncan Murdoch
On 2025-07-28 2:01 p.m., Rui Barradas wrote:
On 7/28/2025 5:30 PM, Daniel Lobo wrote:
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 <ruipbarra...@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@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
Hello,
Here is another explanation on the reason why you should sample from
finite limits that make sense [1].
Ben's answer points you in an acceptable direction. Here is the same
idea with other limits meant to get better floating-point accuracy.
n <- 1e6 # change this at will
mm <- .Machine$double.xmax
u <- runif(n, min = -mm/3, max = mm/3)
hist(u)
[1]
https://math.stackexchange.com/questions/3784691/probability-distribution-of-choosing-a-real-number-at-random
Hope this helps,
Rui Barradas
______________________________________________
R-help@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@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.