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