import random


def select_random_lang(lang_len, tot_egs, random_selection):
    """ Returns a random language index w.r.t
        amount of examples in each language.
        It works based on sampling from a
        discrete distribution, where it returns i
        with prob(i) = (num_egs in lang(i)/ tot_egs).
        tot_egs is sum of lang_len.
    """
    assert(tot_egs > 0)
    rand_int = random.randint(0, tot_egs - 1)
    count = 0
    for l in range(len(lang_len)):
        if random_selection:
            if  rand_int <= (count + lang_len[l]):
                return l
            else:
                count += lang_len[l]
        else:
            if (lang_len[l] > 0):
                return l
    return -1