The standard says that
call random_number(x)
gives x such that 0 <= x < 1
. What are the smallest and largest values for double precision x based on current implementations of random_number
? They will vary by compiler. I tested this with program
program main
implicit none
integer, parameter :: wp = kind(1.0d0), ikind = selected_int_kind(18), &
nran = 10_ikind**9
integer(kind=ikind) :: i,iter
real(kind=wp) :: xmin,xmax,xran
print*,"#ran, wp, tiny(0.0_wp)=",nran,wp,tiny(0.0_wp)
call random_seed()
do iter=1,10
xmin = 1.0_wp
xmax = 0.0_wp
do i=1,nran
call random_number(xran)
xmin = min(xmin,xran)
xmax = max(xmax,xran)
if (xran == 0.0_wp) then
print*,"xran = 0.0_wp for i=",i
exit
end if
end do
print*,"xmin, 1-xmax =",xmin,1.0_wp-xmax
end do
end program main
giving sample results
gfortran
#ran, wp, tiny(0.0_wp)= 1000000000 8 2.2250738585072014E-308
xmin, 1-xmax = 1.2764767021167245E-010 8.5712281716610050E-010
xmin, 1-xmax = 3.5704161849281491E-010 5.8078597486854733E-010
xmin, 1-xmax = 3.5418714627866166E-009 1.9406080076223020E-009
xmin, 1-xmax = 3.8126641843660991E-009 8.9600260544386856E-010
xmin, 1-xmax = 7.0739225588312138E-010 1.5527540364601577E-009
xmin, 1-xmax = 1.0432037456098442E-009 2.4081048266566540E-010
xmin, 1-xmax = 6.7113703483556719E-010 8.0433837368332206E-010
xmin, 1-xmax = 2.2587054449019206E-010 5.0732962275645832E-010
xmin, 1-xmax = 8.7153861905164831E-010 1.9965620268180828E-009
xmin, 1-xmax = 1.8497274822948384E-009 6.2101734865649405E-010
ifort
#ran, wp, tiny(0.0_wp)= 1000000000 8 2.225073858507201E-308
xmin, 1-xmax = 9.313226114783520E-010 4.656632857091836E-010
xmin, 1-xmax = 9.313226114783520E-010 4.656632857091836E-010
xmin, 1-xmax = 1.396983917217528E-009 4.656632857091836E-010
xmin, 1-xmax = 1.396983917217528E-009 1.862647258654704E-009
xmin, 1-xmax = 5.122274363130936E-009 4.656632857091836E-010
xmin, 1-xmax = 4.656613057391760E-010 4.656632857091836E-010
xmin, 1-xmax = 9.313226114783520E-010 4.656632857091836E-010
xmin, 1-xmax = 4.656613057391760E-010 4.656632857091836E-010
xmin, 1-xmax = 2.793967834435056E-009 4.656632857091836E-010
xmin, 1-xmax = 2.328306528695880E-009 2.328308545962443E-009
For ifort it appears that the smallest x or (1-x) is 4.6566e-10. For gfortran it appears that values below 1e-10 are rare, although I did have a run where 4.6566e-11 appeared. It looks like you don’t need to check for the case that x == 0.0_dp in a simulation using random_number
. If you sample from a non-uniform distribution using the inverse cumulative distribution, you can estimate the bounds of those variates given bounds for the uniform variates.