Random Values - AP Computer Science Principles

  1. Which option best describes the sequence produced by a PRNG?
    Deterministic and reproducible with the same seed. Mathematical algorithms ensure consistent behavior across runs.
  2. Find the error: random.uniform(1, 10) gives integers between 1 and 10.
    Correct: Yields floats, not integers. Uniform distribution produces continuous values, not discrete integers.
  3. What is the primary difference between PRNG and TRNG?
    PRNGs are algorithm-based; TRNGs are based on physical processes. PRNGs use math; TRNGs use unpredictable physical phenomena.
  4. Which method resets the state of the random number generator in Java?
    setSeed(long seed). Resets internal state to produce new random sequence.
  5. Find the error: Using the same seed always produces different random sequences.
    Correct: It produces the same sequence. Deterministic algorithms always repeat with identical starting conditions.
  6. What is the typical range of a random number generated by rand() in C++?
    0 to RAND_MAX. RAND_MAX is typically 32767 or larger depending on implementation.
  7. Which method randomizes the order of elements in a list in Python?
    random.shuffle(list). Randomly reorders elements using Fisher-Yates algorithm internally.
  8. Identify the purpose of random.sample(population, k) in Python.
    Returns a k-length list of unique elements from the population. Sampling without replacement ensures no duplicate elements.
  9. What is the output of Math.random() in Java?
    A double value between 0.0 (inclusive) and 1.0 (exclusive). Standard uniform distribution used in most random calculations.
  10. Identify the main function of a seed in random number generation.
    To initialize the random number generator, ensuring reproducibility. Same seed produces identical sequences for testing and debugging.
  11. Choose the correct statement about pseudorandom numbers.
    They are generated by a deterministic algorithm. Mathematical formulas create sequences that appear random but are predictable.
  12. Which function returns a random element from a non-empty sequence?
    random.choice(sequence). Works with any sequence type including lists, tuples, strings.
  13. Identify the type of random number generator that uses physical processes.
    True random number generator (TRNG). Uses hardware entropy like thermal noise or radioactive decay.
  14. What does the method random.choice(list) return in Python?
    A randomly selected element from the list. Picks one item randomly from the sequence with equal probability.
  15. What is the purpose of the srand() function in C++?
    Seeds the random number generator. Initializes the random sequence starting point for reproducibility.
  16. What does random.choices(population, weights) do in Python?
    Selects k elements with given weights. Weighted selection allows different probabilities for each element.
  17. What is the effect of random.seed() without an argument in Python?
    Seeds generator with current time or system state. Uses entropy from system clock or hardware for unpredictability.
  18. Find the error: random(0,1) generates a number between 0 and 1.
    Correct: Generates either 0 or 1, not a decimal. Random integers are discrete values, not continuous decimals.
  19. Which Python function generates a random float between 0.0 and 1.0?
    random.random(). Base function for generating uniform floating-point distributions.
  20. What is a random number generator (RNG)?
    An algorithm that produces a sequence of numbers with no discernible pattern. Uses mathematical algorithms to simulate unpredictable sequences.
  21. Find and correct the mistake: Math.random() * 10 gives a number 1-10.
    Correct: Gives a number 0-10. Multiplication scales from [0,1)[0,1) to [0,10)[0,10) range.
  22. Which programming construct is often used to simulate randomness?
    The random number generator function. Built-in functions like rand() or random() provide pseudorandom values.
  23. Which function is used to generate a random floating-point number in Python?
    random.uniform(a, b). Generates continuous floating-point values within specified bounds.
  24. What type of values does random.randrange(start, stop[, step]) return?
    A random number from the specified range. Similar to range() but returns random value from sequence.
  25. State the formula for generating a random integer between aa and bb inclusive.
    random(a,b)=a+(rand() % (b−a+1))random(a, b) = a + (rand() \, \% \, (b - a + 1)). Modulo operation constrains range to desired integer bounds.
  26. What is the result of random.randint(1, 10) in Python?
    An integer between 1 and 10 inclusive. Inclusive bounds mean both endpoints can be returned.
  27. State the effect of calling random.seed(x) in Python.
    Initializes the random number generator with seed x. Sets starting state for predictable random sequence generation.
  28. What is the result of random.randint(5, 5) in Python?
    Always returns 5. Single value range always returns that exact value.
  29. Identify the range of numbers produced by Math.random() in Java.
    0.0 (inclusive) to 1.0 (exclusive). Standard range for most programming language random functions.

Study these flashcards