Bird
Raised Fist0
SciPydata~20 mins

Random variable generation in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Random Variable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
Output of generating normal random variables
What is the output of this code snippet that generates 3 samples from a normal distribution with mean 0 and standard deviation 1 using scipy.stats?
SciPy
from scipy.stats import norm
samples = norm.rvs(loc=0, scale=1, size=3)
samples_rounded = [round(x, 2) for x in samples]
samples_rounded
A[0.0, 0.0, 0.0]
BA list of 3 float numbers close to 0 but random, e.g., [0.12, -0.45, 1.03]
C[0.5, -1.2, 0.3]
D[1, 1, 1]
Attempts:
2 left
💡 Hint
The output is random but will be three float numbers around zero.
❓ data_output
intermediate
1:00remaining
Shape of generated random variable array
What is the shape of the array generated by this code that samples from a uniform distribution?
SciPy
from scipy.stats import uniform
samples = uniform.rvs(loc=0, scale=10, size=(4, 2))
samples.shape
A(4, 2)
B(2, 4)
C(8,)
D(4,)
Attempts:
2 left
💡 Hint
The size parameter defines the shape of the output array.
🔧 Debug
advanced
1:30remaining
Identify the error in random variable generation code
What error does this code raise when trying to generate samples from a binomial distribution?
SciPy
from scipy.stats import binom
samples = binom.rvs(n=10, p=0.5, size='5')
ATypeError: 'str' object cannot be interpreted as an integer
BValueError: Probability p must be between 0 and 1
CSyntaxError: invalid syntax
DNo error, code runs fine
Attempts:
2 left
💡 Hint
Check the type of the size argument.
🚀 Application
advanced
2:00remaining
Generating and summarizing Poisson random variables
You generate 1000 samples from a Poisson distribution with lambda=3. Which option correctly describes the expected mean and variance of the samples?
SciPy
from scipy.stats import poisson
samples = poisson.rvs(mu=3, size=1000)
mean_val = samples.mean()
var_val = samples.var()
AMean close to 0, variance close to 3
BMean close to 3, variance close to 0
CMean and variance both close to 3
DMean and variance both close to 0
Attempts:
2 left
💡 Hint
For Poisson distribution, mean equals variance.
🧠 Conceptual
expert
1:30remaining
Effect of random seed on reproducibility
Which statement about setting a random seed before generating random variables with scipy.stats is correct?
ASetting the seed has no effect on random variable generation.
BSetting the seed makes the random numbers more random.
CSetting the seed disables randomness and returns constant values.
DSetting the seed ensures the same random numbers are generated every time the code runs.
Attempts:
2 left
💡 Hint
Think about reproducibility in experiments.

Practice

(1/5)
1. What does the rvs method in scipy.stats do?
easy
A. Calculates the mean of a dataset
B. Sorts an array in ascending order
C. Plots a histogram of data
D. Generates random samples from a specified probability distribution

Solution

  1. Step 1: Understand the purpose of rvs

    The rvs method is used to create random values that follow a given distribution.
  2. Step 2: Compare with other options

    Calculating mean, plotting, or sorting are different functions and not related to rvs.
  3. Final Answer:

    Generates random samples from a specified probability distribution -> Option D
  4. Quick Check:

    rvs = random samples [OK]
Hint: Remember: rvs means random variable samples [OK]
Common Mistakes:
  • Confusing rvs with statistical calculations like mean
  • Thinking rvs plots data
  • Assuming rvs sorts data
2. Which of the following is the correct way to generate 5 random samples from a normal distribution with mean 0 and standard deviation 1 using scipy.stats?
easy
A. norm.generate(5, mean=0, sd=1)
B. norm.rvs(loc=0, scale=1, size=5)
C. stats.norm.sample(0, 1, 5)
D. norm.random(mean=0, std=1, count=5)

Solution

  1. Step 1: Recall the correct method and parameters

    In scipy.stats.norm, rvs generates samples. Parameters are loc for mean, scale for std deviation, and size for number of samples.
  2. Step 2: Check each option

    Only norm.rvs(loc=0, scale=1, size=5) uses correct method rvs and correct parameter names.
  3. Final Answer:

    norm.rvs(loc=0, scale=1, size=5) -> Option B
  4. Quick Check:

    Correct method and parameters = norm.rvs(loc=0, scale=1, size=5) [OK]
Hint: Use rvs with loc, scale, and size for sampling [OK]
Common Mistakes:
  • Using wrong method names like random or sample
  • Mixing parameter names like mean instead of loc
  • Incorrect order or missing size parameter
3. What will be the output type of the following code?
from scipy.stats import uniform
samples = uniform.rvs(loc=10, scale=5, size=3)
print(type(samples))
medium
A. <class 'numpy.ndarray'>
B. <class 'list'>
C. <class 'tuple'>
D. <class 'int'>

Solution

  1. Step 1: Understand output of rvs with size>1

    When size is more than 1, rvs returns a NumPy array of samples.
  2. Step 2: Check the printed type

    Printing type(samples) will show <class 'numpy.ndarray'>.
  3. Final Answer:

    <class 'numpy.ndarray'> -> Option A
  4. Quick Check:

    rvs with size=3 returns ndarray [OK]
Hint: rvs with size>1 returns numpy array [OK]
Common Mistakes:
  • Assuming output is a list instead of ndarray
  • Thinking output is a single number
  • Confusing tuple with array
4. Identify the error in the following code snippet:
from scipy.stats import binom
samples = binom.rvs(n=10, p=0.5, size='5')
medium
A. Parameter 'size' should be an integer, not a string
B. Parameter 'p' must be greater than 1
C. Parameter 'n' should be a float, not int
D. Missing import for numpy

Solution

  1. Step 1: Check parameter types for rvs

    The size parameter expects an integer or tuple of integers, not a string.
  2. Step 2: Analyze given parameters

    Here, size='5' is a string, which will cause a TypeError.
  3. Final Answer:

    Parameter 'size' should be an integer, not a string -> Option A
  4. Quick Check:

    size must be int, not string [OK]
Hint: Size must be int or tuple, never string [OK]
Common Mistakes:
  • Passing size as string instead of int
  • Thinking p must be >1 (it's a probability 0-1)
  • Assuming n must be float
5. You want to simulate the number of successes in 20 trials where each trial has a 30% chance of success. Which scipy.stats function call correctly generates 10 such random samples?
hard
A. binomial.rvs(20, 0.3, 10)
B. binom.rvs(trials=20, prob=0.3, samples=10)
C. binom.rvs(n=20, p=0.3, size=10)
D. binom.sample(n=20, p=0.3, size=10)

Solution

  1. Step 1: Identify correct distribution and parameters

    The binomial distribution models number of successes in fixed trials. Parameters are n for trials, p for success probability, and size for sample count.
  2. Step 2: Check each option's correctness

    Only binom.rvs(n=20, p=0.3, size=10) uses correct function binom.rvs and correct parameter names.
  3. Final Answer:

    binom.rvs(n=20, p=0.3, size=10) -> Option C
  4. Quick Check:

    binom.rvs with n, p, size correct = binom.rvs(n=20, p=0.3, size=10) [OK]
Hint: Use binom.rvs with n, p, size for binomial samples [OK]
Common Mistakes:
  • Using wrong parameter names like trials or prob
  • Using non-existent functions like binomial.rvs
  • Confusing sample with rvs method
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard