import random


def random_walk(n, m):
    """

    Args:
        n (int): The threshold value at which the function records
            the step count when the absolute value of the position
            exceeds this number.
        m (int): The number of times the simulation is run before returning.

    Returns:
        float: The average step count needed to reach the threshold.

    """

    choices = [-1, 1]
    step_counts = []
    for i in range(m):
        position = 0
        step_count = 0
        while abs(position) <= n:
            position += random.choice(choices)
            step_count += 1

        step_counts.append(float(step_count))

    return sum(step_counts) / float(m)