c----------------------------------------------------------- c Demonstration program for Fortran use of C routines c 'drand48' and 'srand48' which provide the same c functionality as 'rand' and 'srand' on the SGI c machines, but which are more portable. In particular, c these routines are available on Linux systems. c c See the source code 'drand48.c' for the implementation c of the Fortran interface to the C routines, as well as c the 'Makefile' for compilation and linking information. c c This program generates 'nrand' random numbers on the c unit interval, bins them into 'nbin' uniform-subintervals, c then outputs the fraction of numbers generated in each c bin. c----------------------------------------------------------- program trand48 implicit none c----------------------------------------------------------- c Declaration of random number generator c----------------------------------------------------------- real*8 drand48 c----------------------------------------------------------- c Control parameters and bin array c----------------------------------------------------------- integer nrand parameter ( nrand = 100 000 ) integer nbin parameter ( nbin = 10 ) integer bin(nbin) c----------------------------------------------------------- c Locals c----------------------------------------------------------- integer i, ibin c----------------------------------------------------------- c Random number generator can be 'seeded' (initialized) c via 'srand48' which takes a single integer argument c (the seed). c----------------------------------------------------------- call srand48(2**20-1) write(0,*) 'Generating ', nrand, ' random numbers' write(0,*) 'Using ', nbin, ' bins' write(0,*) 'Relative frequencies:' do i = 1 , nbin bin(i) = 0.0d0 end do do i = 1 , nrand ibin = max(1,min(nbin * drand48() + 1,nbin)) bin(ibin) = bin(ibin) + 1 end do do i = 1 , nbin write(*,fmt='(f6.3)') bin(i) / (1.0d0 * nrand) end do stop end