Created
December 17, 2014 18:01
-
-
Save shane5ul/ed9b22f5fd7ac28c6f77 to your computer and use it in GitHub Desktop.
Using Fortran90's intrinsic random number generator, using either a specified seed or using the system clock
This file contains hidden or 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment