PROGRAM Dice_Roll
!-----------------------------------------------------------------------
! This program uses a random number generator to simulate rolling a
! pair of dice several times, counting the number of times a specified
! number of spots occurs.  Identifiers used are:
!  Spots    : number of spots to be counted
!  Count    : number of times Spots occurred
!  NumRolls : number of rolls of dice
!  R1, R2   : two random real numbers in the range 0 to 1
!  Die_1,
!  Die_2    : number of spots on die #1, #2, respectively
!  Pair     : sum of Die_1 and Die_2 = total # of spots on the dice
!  Roll     : counts dice rolls
!  Response : user response
!
! Input:  NumRolls, Spots, Response
! Output: User prompts, and the relative frequency of the number of
!         spots
!-----------------------------------------------------------------------

  IMPLICIT NONE
  INTEGER :: Spots, Count, NumRolls, Die_1, Die_2, Pair, Roll
  REAL :: R1, R2
  CHARACTER(1) :: Response

  WRITE (*, '(1X, A)', ADVANCE = "NO") &
        "Enter number of times to roll the dice: "
  READ *, NumRolls

  ! Seed the random number generator
  CALL RANDOM_SEED

  ! Begin the simulation
  DO
     WRITE (*, '(1X, A)', ADVANCE = "NO") &
           "Enter number of spots to count: "
     READ *, Spots
     Count = 0
     DO Roll = 1, NumRolls
        CALL RANDOM_NUMBER(R1)
        CALL RANDOM_NUMBER(R2)
        Die_1 = 1 + INT(6*R1)
        Die_2 = 1 + INT(6*R2)
        Pair = Die_1 + Die_2
        IF (Pair == Spots) Count = Count + 1
     END DO

     PRINT '(1X, "Relative frequency of", I3, " was", F6.3)', &
           Spots,  REAL(Count) / REAL(NumRolls)

     WRITE (*, '(/ 1X, A)', ADVANCE = "NO") "More rolls (Y or N)? "
     READ *, Response
     IF (Response /= "Y") EXIT
  END DO

END PROGRAM Dice_Roll
