In Java, Math.random() returns a double d with 0.0 <= d < 1.0. Which expression produces a random integer from 1 to 6, each value equally likely?
A(int) (Math.random() * 6)
B(int) (Math.random() * 6) + 1
C(int) (Math.random() * 7)
D(int) Math.random() * 6 + 1
Math.random() * 6 lies in [0, 6), and casting to int gives 0 through 5. Adding 1 shifts the range to 1 through 6. Option A gives 0 to 5. Option C gives 0 to 6. Option D casts Math.random() to int first, which is always 0, so it always gives 1.
Question 2 Multiple Choice
A program calls random.seed(42) and then prints five calls to random.randint(1, 100). It is run twice on the same Python version. What happens?
AThe two runs print different numbers, because the values are random
BThe two runs print the same five numbers
CThe second run raises an error, because a seed can be used only once
DBoth runs print 42 five times
A pseudorandom generator is deterministic. The same seed starts it in the same internal state, so it produces the same sequence. Programmers use this to make bugs in simulations repeatable.
Question 3 True / False
Running a simulation with more trials always produces the exact true probability.
TTrue
FFalse
Answer: False
More trials usually bring the estimate closer to the true value, as the law of large numbers predicts, but any finite run still has random error. A simulation also inherits any error built into its model's assumptions.
Question 4 Short Answer
A city simulates traffic at a new intersection instead of building it and watching. Give one advantage of the simulation and one limitation.
Think about your answer, then reveal below.
Model answer: Advantage: it can test many designs quickly, cheaply and safely, including rare or dangerous situations. Limitation: it simplifies reality (for example, it assumes drivers behave in fixed ways), so its results can be wrong or biased if those assumptions do not match real behavior.
Simulations trade realism for speed and safety. Their value depends on how well the model's assumptions match the real system, which is why results are checked against real data when possible.