import random


def get_next_random(value, max_value, min_value, max_delta):
    """
    Calculates and returns the next sensor value based on the current one, the
    limits and the maximum delta.

    Args:
        value (Integer): the current value of the sensor.
        max_value (Integer): the maximum value that the sensor can have.
        min_value (Integer): the minimum value that the sensor can have.
        max_delta (Integer): the maximum delta for the next value.

    Returns:
        The next sensor value based on the current one.
    """
    # Determine if sensor delta should be added or substracted.
    if value == max_value:
        add = False
    elif value == min_value:
        add = True
    else:
        add = random.random() > 0.5

    # Calculate a new delta.
    delta = random.randint(0, max_delta)

    # Apply the delta.
    if add:
        value += delta
    else:
        value -= delta
    if value > max_value:
        value = max_value
    elif value < min_value:
        value = min_value

    return value