Generalized Optimal Risk Allocation: Foraging and Antipredator Behavior in a Fluctuating Environment