Commentary below slide.
| Previous slide | Next slide | Back to first slide | View graphic version |
Look at the program above. The intent of this program is to simulate 500 tosses of a fair coin.
For the variable heads to increment, the expression ( rand.nextInt() % 2) must return a 1. Can the expression ( rand.nextInt() % 2) return a 1?
Answer. Yes, the expression ( rand.nextInt() % 2) returns a 0 or a 1.
The program simulates a coin tossing experiment. 500 tosses are made. Probability states that a fair coin should come up heads as often as it does tails. But the results shown above do not seem very fair.
If we run the program again, will we get exactly the same results?
No, the results will not be exactly the same because the seed value for the random number algorithm is determined by the time at which the program runs.