sample Subroutine

private subroutine sample(this, tgt, prob)

draw a random element from 1:size(this%probs) with the probabilities listed in prob @param[in] tgt on return, this is a random number in the sampling range of this @param[out] prob on return, the probability of picking tgt

Type Bound

AliasSampler_t

Arguments

Type IntentOptional Attributes Name
class(AliasSampler_t), intent(in) :: this
integer, intent(out) :: tgt
real(kind=dp), intent(out) :: prob

Contents

Source Code


Source Code

    subroutine sample(this, tgt, prob)
        class(AliasSampler_t), intent(in) :: this
        integer, intent(out) :: tgt
        real(dp), intent(out) :: prob

        ! empty samplers don't return anything - since probs defaults to null(), this check is safe
        if (.not. associated(this%probs%ptr)) then
            tgt = 0
            prob = 1.0_dp
            return
        end if
        ! get the drawn number from the alias table
        tgt = this%table%sample()
        ! and its probability
        prob = this%probs%ptr(tgt)
    end subroutine sample