Randomly generate set of size M from array of size N
Your understanding of Fisher-Yates is incorrect: this is precisely Fisher-Yates. It merely differs from some implementations in accumulating at the start of the array rather than the end. As such the main part of the body is correct (and uniform, modulo artefacts of the RNG).

The alternative way round, accumulating at the end, would be

    for i in range(m):
        j = len(arr)-1-i
        rand = random.randint(0, j)
        arr[j], arr[rand] = arr[rand], arr[j]

    return arr[-m:]




However, I find the special case a bit confusing. if len(arr) &lt; m I would expect an exception, and since that isn't the case I think there should be a comment to document the behaviour.