There are three steps involved:
- Allocating the seed
- Setting the seed using either (a) the system clock, or (b) a user-defined number
- Harvesting the random numbers
Here is a minimal program which demonstrates the use of Fortran's intrinsic random number generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! | |
! Program to demonstrate the use of Fortran 90's intrinsic random_number() | |
! | |
program main | |
integer :: values(8), k, nseed | |
integer, dimension(:), allocatable :: seed | |
real(8) :: r(10) | |
! | |
! Step 1: To over-ride "default seed", you have to initialize it first | |
! | |
call random_seed(size = k) | |
allocate(seed(1:k)) | |
! | |
! Step 2a: You may use the "milliseconds" from the system clock to set it automatically | |
! | |
call date_and_time(values=values) ! t | |
seed(:) = values(8) | |
! | |
! Step 2b: Or set it to a value you want (to reproduce errors for example) | |
! UNCOMMENT TO USE | |
! | |
! nseed = 5 | |
! seed(:) = nseed | |
! call random_seed(put = seed) | |
! | |
! A call to random_number populates the "matrix" "r" in general | |
! | |
call random_number(r) | |
do i = 1, 10 | |
write(*,'(f7.4)') r(i) | |
enddo | |
end program main |