Random Values - AP Computer Science Principles
- Which option best describes the sequence produced by a PRNG?
- Deterministic and reproducible with the same seed. Mathematical algorithms ensure consistent behavior across runs.
- 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.
- 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.
- Which method resets the state of the random number generator in Java?
- setSeed(long seed). Resets internal state to produce new random sequence.
- 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.
- 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.
- Which method randomizes the order of elements in a list in Python?
- random.shuffle(list). Randomly reorders elements using Fisher-Yates algorithm internally.
- 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.
- 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.
- 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.
- Choose the correct statement about pseudorandom numbers.
- They are generated by a deterministic algorithm. Mathematical formulas create sequences that appear random but are predictable.
- Which function returns a random element from a non-empty sequence?
- random.choice(sequence). Works with any sequence type including lists, tuples, strings.
- 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.
- 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.
- What is the purpose of the srand() function in C++?
- Seeds the random number generator. Initializes the random sequence starting point for reproducibility.
- What does random.choices(population, weights) do in Python?
- Selects k elements with given weights. Weighted selection allows different probabilities for each element.
- 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.
- 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.
- Which Python function generates a random float between 0.0 and 1.0?
- random.random(). Base function for generating uniform floating-point distributions.
- 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.
- Find and correct the mistake: Math.random() * 10 gives a number 1-10.
- Correct: Gives a number 0-10. Multiplication scales from to range.
- Which programming construct is often used to simulate randomness?
- The random number generator function. Built-in functions like rand() or random() provide pseudorandom values.
- 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.
- 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.
- State the formula for generating a random integer between and inclusive.
- . Modulo operation constrains range to desired integer bounds.
- 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.
- 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.
- What is the result of random.randint(5, 5) in Python?
- Always returns 5. Single value range always returns that exact value.
- 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.