<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def prefer_replica(nodes: List[DiscoveredNode]) -> Optional[DiscoveredNode]: """ Select a random replica if any are available or fall back to the master. """

    masters = [node for node in nodes if node.state == NodeState.Master]
    replicas = [node for node in nodes if node.state != NodeState.Master]

    if replicas:
        return random.choice(replicas)
    else:
        # if you have more than one master then you're on your own, bud.

        return masters[0]