I am assuming that you are using 8 and 9 to make the range of values never meet the 0-4 range tested for setting the should_beacon_header_val. If that is so, couldn't we just set $rand to 1?