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

Ben Bolker bbo|ker @end|ng |rom gm@||@com
Mon Jul 28 18:17:00 CEST 2025


   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 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.

-- 
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.



More information about the R-help mailing list