I am doing one exercise in the Absolute Java, Chapter 5, exercise 3. And facing with a problem confusing me.
Let me introduce the background first.
The Question is :
In the land of Puzzlevania, Aron, Bob, and Charlie had an argument over which one of them was the greatest puzzler of all time.
To end the argument once and for all, they agreed on a duel to the death. Aaron was a poor shooter and only hit his target with a probability of 1>3. Bob was a bit better and hit his target with a probability of 1>2. Charlie was an expert marksman and never missed.
A hit means a kill and the person hit drops out of the duel. To compensate for the inequities in their marksmanship skills, the three decided that they would fire in turns, starting with Aaron, followed by Bob, and then by Charlie. The cycle would repeat until there was one man standing, and that man would be the Greatest Puzzler of All Time. An obvious and reasonable strategy is for each man to shoot at the most accurate shooter still alive, on the grounds that this shooter is the deadliest and has the best chance of hitting back.
Write a program to simulate the duel using this strategy. Your program should use
random numbers and the probabilities given in the problem to determine whether
a shooter hits the target. Create a class named
Duelist that contains the dueler’s
name and shooting accuracy, a Boolean indicating whether the dueler is still alive,
and a method
ShootAtTarget ( Duelist target ) that sets the target to dead if
the dueler hits his target (using a random number and the shooting accuracy) and
does nothing otherwise.
Once you can simulate a single duel, add a loop to your program that simulates
10,000 duels. Count the number of times that each contestant wins and print the
probability of winning for each contestant (e.g., for Aaron your program might
output “Aaron won 3,595>10,000 duels or 35.95%”).
An alternate strategy is for Aaron to intentionally miss on his first shot. Modify the
program to accommodate this new strategy and output the probability of winning
for each contestant. Which strategy is better for Aaron: to intentionally miss on the
first shot or to try and hit the best shooter? Who has the best chance of winning,
the best shooter or the worst shooter?
My Question is:
How to use random numbers and the probabilities given in the problem to determine whether a shooter hits the target?
I don’t understand why should use random numbers and probabilities, is there any relationship between them?
Generate a random number (in the range
[0,1)) withMath.random, then simply check that it’s less than the given probability. So if someone had a 20% accuracy, we would havewhich will be true 20% of the time.
I think the best way to understand this is to look at a simple case: consider flipping a coin. How would we simulate such an event? Well there is a 50% chance that it lands on tails, and a 50% chance that it lands on heads. Consequently, if we have a random variable that has a 50% chance of being
trueand a 50% chance of beingfalse, we can use this variable to run the simulation, since it is analogous to the actual coin flip itself, withfalsebeing heads andtruebeing tails (or the other way around, it really doesn’t matter). In this case, we would have something likeWhat is written above works in the same way, but with probabilities other than 50%.
Now if you’re wondering why
Math.random() < 0.5has a 50% chance of beingtrue, you should think about it geometrically.Math.random()returns a random number from0to1– and0.5cuts this interval in half. Therefore, there is a 50% chance that the random number will land to the right of0.5, and a 50% chance that it will land to the left.Since
Math.random() < 0.5is essentially asking “is the random number to the left of.5“, it has a 50% chance of beingtrueand a 50% chance of beingfalse.