!********o*********o*********o*********o*********o*********o*********o**
! In spite of previous claims, this program doesn't work as intended
! with the ifc random-number generator.
!---------------------
! This program singles out a random student in 316 Hammond for the
! instructor to call on.
!
! This program is intended to demonstrate two things.  One is the use
! of SetSpecificRandomSeed so that we get a different random number
! every time we run the program.  The other is the advance="no"
! construct, which allows us to build a line of output piecemeal.
!
! Inputs
!   (none).
!
! Outputs
!   A diagram of the lab, with the poor sap singled out.
!---------------------
! Author:   Bob Harris
! Class:    CMPSC 201F
! Section:  1234
! Date Due: 19 Nov 2003
!********o*********o*********o*********o*********o*********o*********o**

program inquisitor

implicit none

integer, parameter :: rows        = 5
integer, parameter :: seatsPerRow = 8
real               :: r
integer            :: student, studentRow, studentSeat
integer            :: row, seat
integer            :: char, totalChars

!----------
! choose a student at random
!----------

! use the system clock to 'seed' the pseudo-random number generator

call SetSpecificRandomSeed (0)

! choose a random student;  the variable 'student' will be a random
! integer in the range 0..rows*seatsPerRow-1

call RANDOM_NUMBER(r)
student = int(rows * seatsPerRow * r)

! figure out the row and seat the student is sitting in, in the range
! 1..rows and 1..seatsPerRow

studentRow  = (student/seatsPerRow)    + 1
studentSeat = mod(student,seatsPerRow) + 1

!----------
! print the class diagram, singling out that student
!----------

! print the front of the classroom (centered)

totalChars = 3 + 2 + 5*seatsPerRow

do char=1,(totalChars-7)/2
    write (*,"(A1)",advance="no") " "
end do
write (*,*) "(front)"

write (*,*)

write (*,"(A3,A2)",advance="no") "   ","  "
do seat=1,seatsPerRow
    write (*,"(I5)",advance="no") seat
end do
write (*,*)                ! finish off the line

! print each row of the classroom

do row=1,rows

    ! 'draw' the table for this row

    write (*,"(A3,A2)",advance="no") "   ","  "

    do seat=1,seatsPerRow
        if (seat == 1) then
            write (*,"(A5)",advance="no") " ####"
        else
            write (*,"(A5)",advance="no") "#####"
        end if
    end do

    write (*,*)                ! finish off the line

    ! 'draw' the chairs in this row

    write (*,"(I3,A2)",advance="no") row,": "

    do seat=1,seatsPerRow
        if ((row /= studentRow).or.(seat /= studentSeat)) then
            write (*,"(A5)",advance="no") " [  ]"
        else
            write (*,"(A5)",advance="no") " [;(]"
        end if
    end do

    write (*,*)                ! finish off the line

    ! 'draw' space to roll a chair behind this row

    write (*,*)

end do

! print the back of the classroom (centered)

do char=1,(totalChars-6)/2
    write (*,"(A1)",advance="no") " "
end do

write (*,*) "(back)"

contains

!-----------------------------------------------------------------------
! SetSpecificRandomSeed--
!   Seed the random number generator with your choice of seed.
!---------------------
! This routine fails to have the desired effect under ifc.  It causes
! ifc's generator to go into a mode where it just adds a constant to
! the previous random number, modulo 1.
!---------------------
! Arguments
!   sequenceChoice:  The initial seed for the random number generator.
!                    If this is zero, we use the system clock instead.
!-----------------------------------------------------------------------

subroutine SetSpecificRandomSeed (sequenceChoice)
    integer, intent(in)                :: sequenceChoice
    integer                            :: seedSize
    integer, allocatable, dimension(:) :: seedArray
    integer                            :: count, countrate, countmax
    integer                            :: magic
    integer                            :: index

    ! find out how big the random number generator's seed is;  the
    ! fortran standard allows the seed to be different sizes on
    ! different operating systems or compilers

    call random_seed (size=seedSize)

    ! allocate an array big enough to hold the entire seed

    allocate (seedArray(seedSize))

    if (sequenceChoice /= 0) then
        magic = sequenceChoice
    else
        call system_clock (count, countrate, countmax)
        magic = count
    end if

    ! fill the array with a deterministic sequence, based on the
    ! sequence-choice number;  note that is it necessary to write
    ! a value to each element of the seed array, and that value
    ! must depend on the sequence-choice number

    do index=1,seedSize
        seedArray(index) = magic
        magic            = (5*magic)+1
    end do

    ! initialize the random number generator with our seed array

    call random_seed (put=seedArray)

end subroutine SetSpecificRandomSeed

end program inquisitor

