The main documentation of the Generate_Even_Distribution Procedure contains additional explanation of this code listing.
subroutine Generate_Even_Distribution (Vector, NItems)
! Input variable.
! Number of items to be evenly distributed.
type(integer), intent(in) :: NItems
! Input/Output variable.
! Vector to hold the even distribution.
type(integer,1) :: Vector
! Internal variables.
type(integer) :: extra ! Leveling variable.
type(integer) :: i ! Loop counter.
type(integer) :: NSlots ! Number of slots available in the Vector.
!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
! Verify requirements.
VERIFY(Valid_State(Vector),5) ! Vector is valid.
VERIFY(Valid_State(NItems),5) ! NItems is valid.
VERIFY(NItems>=SIZE(Vector),5) ! NItems is big enough.
! Generate the even distribution.
NSlots = SIZE(Vector)
do i = 1, NSlots-1
Vector(i) = NItems / NSlots
end do
Vector(NSlots) = NItems - SUM(Vector(1:NSlots-1))
extra = Vector(NSlots) - NItems / NSlots
do i = 1, extra
Vector(i) = Vector(i) + 1
end do
Vector(NSlots) = NItems - SUM(Vector(1:NSlots-1))
! Verify guarantees.
VERIFY(Valid_State(Vector),5) ! Vector is valid.
VERIFY(SUM(Vector)==NItems,5) ! Vector sum is correct.
VERIFY(MAXVAL(Vector)-MINVAL(Vector)<=1,5) ! Vector is evenly distributed.
return
end subroutine Generate_Even_Distribution