Skip to content
Marlbridge

AP® Computer Science Principles · Unit 3 · Topic 3.15

Random Values: Revision Notes (Computer Science Principles 3.15)

One-page recap of random values: inclusive range, counting possible values, results of expressions, equal likelihood of a single call, probabilities for events, and random list indices.

Recap

  • RANDOM(a, b) returns a random integer from a to b inclusive. Each value is equally likely.
  • Number of possible values: b − a + 1.
  • Each execution may give a different result.
  • To find the possible results of an expression, list each possible RANDOM value and apply the expression.
  • A sum or other combination of two calls is not uniform.
  • Probability of an event: favourable values ÷ number of possible values.
  • Random list element: aList[RANDOM(1, LENGTH(aList))].

Key relationships

Goal Expression Possible values
Die roll RANDOM(1, 6) 1 to 6
Coin flip RANDOM(1, 2) (1 = heads) 1 or 2
Multiple of 5 from 10 to 50 RANDOM(2, 10) * 5 10, 15, …, 50 (9 values)
Odd numbers 1, 3, 5 2 * RANDOM(1, 3) - 1 1, 3, 5
30% chance RANDOM(1, 10) ≤ 3 true 3 times in 10
Random list item aList[RANDOM(1, LENGTH(aList))] Every element, no error

Assumptions

  • Each RANDOM call is independent of the others.
  • Every integer in the range is equally likely for a single call.
  • Indices start at 1.

Method for possible-results questions

  1. Write the list of values each RANDOM call can return (count them: b − a + 1).
  2. Substitute each value into the rest of the expression.
  3. Collect the set of results. Remove repeats.
  4. If asked about likelihood, count how many ways each result can happen.

Mistakes to avoid

  • Forgetting the end values are included.
  • Counting b − a possible values.
  • Treating the sum of two calls as uniform.
  • Using LENGTH(aList) + 1 or 0 as an index limit.
  • Judging a random program after one run.

Worked mini-example

Possible results of RANDOM(1, 4) * 3: the values 1, 2, 3, 4 become 3, 6, 9, 12. So the set is {3, 6, 9, 12}. Four values, each with probability 1/4. The result is never 4 or 5 or 10.

Quick self-check

  1. How many values can RANDOM(5, 12) return?
  2. What are the possible results of RANDOM(1, 3) + 10?
  3. What is the probability that RANDOM(1, 5) > 3?
Answers
  1. 12 − 5 + 1 = 8.
  2. 11, 12 or 13.
  3. Values 4 and 5 qualify: 2 out of 5, which is 2/5 or 40%.

Where marks are won and lost

Typical questions ask which values an expression could produce, or which code could display a particular value. List the possible RANDOM values, apply the expression to each and read off the set. Another kind gives a conditional using RANDOM and asks the probability of a branch or the possible outputs. Look for off-by-one slips in ranges (inclusive ends) and in list indices. You may also be asked why a random program should be tested more than once.

Back to the full study guide for worked examples and explanations.

AP® and Advanced Placement® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this website. Marlbridge is an independent tuition provider. It is not an authorized AP course provider, an AP school or an exam centre, and these resources are not official College Board materials. Reading them or doing simulations does not meet the laboratory requirement of an AP science course.