Fix a possible panic in the weight randomization algorithm