Questions about Call Random_number in gfortran

What does actually say this? The Gnu Fortran doc page of RANDOM_INIT that you linked above does not seem to say anything like it.

You can simply use rnd = 1 - rnd if your algorithm works for 0 < rnd <= 1.

1 Like

@fortranchris
I don’t understand why you have this zero problem; if you use either 32-bit or 64-bit reals, zero is a possible random_number value.
If your algorithm requires 0 < x < 1, then put a filter on the random number generator to exclude zero, no matter how small the possibility.

The following is a way of getting a different start random number for each run. (although having the same random number sequence can be good for program testing.)

   call create_seed
   end 
   
   subroutine create_seed
     integer :: values(8), i
     integer, allocatable :: seed_list(:)
     real    :: x
     integer :: max_seed_value = ( huge(i)-1 )
     
     call date_and_time ( values=values )
     write (*,*) 'miliseconds =',values(8)
     do i = 1,values(8)
       call my_random_number (x)
     end do

     call random_seed ( size = nseed )
     write (*,*) 'seed size =',nseed
     allocate ( seed_list(nseed) )
     do i = 1,nseed
       call my_random_number (x)
       seed_list(i) = int ( x * max_seed_value )
     end do

     call random_seed ( put = seed_list )
   end subroutine create_seed

   subroutine my_random_number (x)
   real :: x
   do
     call random_number (x)
     if ( x > 0 ) exit
   end do
   end subroutine my_random_number

I have done capacity modelling for many years, starting with a 2-byte integer random number generator. This crude random generator worked well, except for extremely rare events. However when testing for the occurrence of an extremely rare event, it is possibly better to restructure the approach, by using a negative exponential distribution to estimate the time to the next event.

The gfortran PRNG documentation is spread over three different pages: