A Necessary and Sufficient Condition for Consensus Over Random Networks