I've been sharpening up my C syntax, which has differing standards than
fortran. Within the context of a compare routine for qsort, a question
came up about when the difference between two integers busts the datatype.
I wanted to confirm that it happens about one eighth of the time, and to
that end, I wrote the following prog:
implicit none
integer, parameter :: i13 = selected_int_kind(13)
integer(i13), parameter:: i_max = 63
integer(i13), parameter:: i_tot = i_max*2 +1
! integer(i13), parameter:: sides = 8
integer(i13), parameter:: sides = i_tot
integer(i13), parameter:: trials = 10
integer(i13), dimension(trials)::C
integer(i13), dimension(trials)::F
integer(i13):: b, i, &
diff2, c3, c4
real:: harvest, diff, t3, t4
! seed random num generator
CALL init_random_seed
! prime the pump
call random_number(harvest)
b = 3 + nint(10*harvest)
do i=1,b
call random_number(harvest)
print *, i, harvest
end do
do i = 1, trials
call random_number(harvest)
b = min(max(1,ceiling(harvest*sides)),sides)
C(i) = b
end do
print *, 'C ='
print *, C
print *, 'F ='
do i = 1, trials
F(i) = C(i) - i_max
end do
print *, F
! time it
call system_clock (c3)
call cpu_time (t3)
call cpu_time (t4)
call system_clock (c4)
diff=t4-t3
diff2=c4-c3
! output
print *, "system clock was ", diff
print *, "cpu time was ", diff2
! end output
contains
SUBROUTINE init_random_seed()
INTEGER :: i, n, clock
INTEGER, DIMENSION(:), ALLOCATABLE :: seed
CALL RANDOM_SEED(size = n)
print *, "n=", n
ALLOCATE(seed(n))
CALL SYSTEM_CLOCK(COUNT=clock)
print *, "clock=", clock
seed = clock + 37 * (/ (i - 1, i = 1, n) /)
CALL RANDOM_SEED(PUT = seed)
print *, "seed= ", seed
DEALLOCATE(seed)
END SUBROUTINE
end program
! gfortran -o pop -Wall freeformat56.f95
n= 1
clock= 131479696
seed= 131479696
1 0.481321
2 0.127049
3 0.334631
4 0.179291
5 7.131684E-02
6 0.326400
7 0.298898
8 0.318636
9 0.554298
10 0.904766
11 0.582346
12 0.595471
C =
93 42 15
109 99
93 57 71
111 80
F =
30 -21 -48
46 36
30 -6 8
48 17
system clock was 0.00000
cpu time was 0
Press RETURN to close window . . .
There are a lot of spare parts here for diagnosis, but does this prog give
equiprobable integers on the interval of [-i_max , i_max] ?
--
It doesn't take a majority to make a rebellion; it takes only a few
determined leaders and a sound cause.
H. L. Mencken