Skip to content

Instantly share code, notes, and snippets.

@DSCF-1224
Created August 18, 2019 01:49
Show Gist options
  • Save DSCF-1224/2a85ce0779558755bd82649f9ad39a36 to your computer and use it in GitHub Desktop.
Save DSCF-1224/2a85ce0779558755bd82649f9ad39a36 to your computer and use it in GitHub Desktop.
generate random characters using subroutine `RANDOM_NUMBER`
program hello
use, intrinsic :: iso_fortran_env
implicit none
integer(INT8), parameter :: ichar_a = ichar("a")
integer(INT8), parameter :: ichar_z = ichar("z")
integer(INT8), parameter :: ichar_width_az = ichar_z - ichar_a + 1
integer :: itr
integer(INT8) :: val_chr
real(REAL32) :: rnd
real(REAL64) :: average
real(REAL64) :: deviation
real(REAL64) :: stderr
integer(INT32), dimension(ichar_a:ichar_z) :: data_ichar
data_ichar(:) = 0_INT8
do itr = 1, 10 ** 6, 1
call random_number(rnd)
val_chr = ichar_a + int(rnd * ichar_width_az)
data_ichar(val_chr) = data_ichar(val_chr) + 1
end do
average = real(sum(data_ichar(:), dim=1), kind=REAL64) / ichar_width_az
stderr = 0.0_REAL64
do itr = ichar_a, ichar_z, 1
deviation = real(data_ichar(itr), kind=REAL64) - average
write(unit=output_unit, fmt='(I3,1X,A1,1X,I10,1X,F10.2)', advance='yes') itr, char(itr), data_ichar(itr), deviation
stderr = stderr + deviation * deviation
end do
stderr = sqrt( stderr / (size(data_ichar(:), dim=1) - 1) )
print *, average, stderr
end program hello
! EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment