2. If an equal likelihood of each of several discrete events exists, in a simulation we can generate a random integer to indicate the choice. For example, in a simulation of a pollen grain moving in a fluid, suppose at the next time step the grain is just as likely to move in any direction—north, east, south, west, up, or down—in a three-dimensional (3D) grid. A probability of 1/6 exists for the grain to move in any of the six directions. With these equal probabilities, we can generate a uniformly distributed integer between 1 and 6 to indicate the direction of movement.
Suppose in a simulation involving animal behavior, a lab rat presses a food lever (FOOD = 1) 15% of the time, presses a water lever (WATER = 2) 20% of the time, and does neither (NEI- THER = 3) the remainder of the time. For the simulation, we consider the range split into three parts, and again generate a uniformly distributed ran- dom floating-point number from 0.0 to 1.0. If the number is less than 0.15, which occurs 15% of the time, we assign FOOD = 1 to the rat’s action. For 20% of the time, the uniformly distributed random number is greater than or equal to 0.15 and less than 0.35. With a random number in this range, we make the rat’s action be WATER = 2. A random number is greater than or equal to 0.35 with a probability of 65%. In such a case, we assign NEITHER = 3 to the rat’s action. Thus, with rand being a uniformly distributed random floating-point number from 0.0 to 1.0, we em- ploy the following logic for determination of the rat’s action:
if a random number, rand, is < 0.15
the rat presses the food lever
else if rand < 0.35 (i.e., 0.15 ? rand < 0.35)
the rat presses the water lever
else (i.e., 0.35 ? rand)
the rat does neither
Using the above logic used in determining the action of a rat, write a segment to return FOOD, WATER, or NEITHER, depending on the value of the random number. ?