module solution_5_3
public  ::  shuffle
contains
! Solution 5/3
 subroutine shuffle(cards)
    integer, dimension(:), intent(out):: cards
    integer :: left, choice, i, temp
    real    :: r
    cards = (/ (i, i=1,size(cards)) /)    ! Initialize deck.
    do left = size(cards),1,-1   ! Loop over number of cards left.
       call random_number(r)     ! Draw a card
       choice = r*left + 1       !    from remaining possibilities
       temp = cards(left)        !    and swap with last
       cards(left) = cards(choice)!    one left.
       cards(choice) = temp
    end do
 end subroutine shuffle
end module solution_5_3
