import random


def random_bits(bit_ranges):
    """Generate random binary mask with ones in the specified bit ranges.

    Each bit_ranges is a list of tuples of lower and upper limits of bit
    positions will be fuzzed. The limits are included. Random amount of bits
    in range limits will be set to ones. The mask is returned in decimal
    integer format.
    """
    bit_numbers = []
    # Select random amount of random positions in bit_ranges
    for rng in bit_ranges:
        bit_numbers += random.sample(range(rng[0], rng[1] + 1),
                                     random.randint(0, rng[1] - rng[0] + 1))
    val = 0
    # Set bits on selected positions to ones
    for bit in bit_numbers:
        val |= 1 << bit
    return val