Random sampling distributions help us understand how data behaves when we pick random samples. They show the variety of possible results from repeated random picks.
Random sampling distributions in NumPy
numpy.random.choice(a, size=None, replace=True, p=None) numpy.random.normal(loc=0.0, scale=1.0, size=None) numpy.random.binomial(n, p, size=None)
choice picks random items from an array.
normal creates samples from a bell-shaped curve.
binomial simulates yes/no outcomes repeated n times.
import numpy as np sample = np.random.choice([1, 2, 3, 4, 5], size=3, replace=False) print(sample)
import numpy as np sample = np.random.normal(loc=10, scale=2, size=5) print(sample)
import numpy as np sample = np.random.binomial(n=10, p=0.5, size=4) print(sample)
This program simulates rolling a die 1000 times and shows how often each face appears as a percentage.
import numpy as np # Simulate rolling a 6-sided die 1000 times rolls = np.random.choice([1, 2, 3, 4, 5, 6], size=1000, replace=True) # Calculate frequency of each face faces, counts = np.unique(rolls, return_counts=True) # Show results as percentages percentages = counts / 1000 * 100 for face, percent in zip(faces, percentages): print(f"Face {face}: {percent:.1f}%")
Random sampling results will change each time you run the code because of randomness.
Use replace=False in choice to avoid picking the same item twice.
Sampling distributions help us see the spread and patterns in random data.
Random sampling distributions show possible outcomes from repeated random picks.
Use numpy functions like choice, normal, and binomial to create samples.
They help us understand data behavior and variability in real life.
Practice
numpy.random.choice function do in random sampling?Solution
Step 1: Understand the purpose of
This function is used to randomly pick elements from an input array or list, optionally with replacement.numpy.random.choiceStep 2: Compare with other options
Options A, B, and C describe different numpy functions or operations unrelated to random selection from an array.Final Answer:
It selects random elements from a given array or list. -> Option AQuick Check:
numpy.random.choice= random selection [OK]
- Confusing choice with normal distribution generator
- Thinking choice creates sequences
- Assuming choice calculates statistics
Solution
Step 1: Recall the numpy binomial function signature
The correct syntax isnumpy.random.binomial(n, p, size)wherenis number of trials,pis success probability, andsizeis number of samples.Step 2: Check each option
numpy.random.binomial(10, 0.3, size=5)matches the correct order and uses the keywordsize. numpy.binomial(10, 0.3, 5) misses.random. numpy.random.binomial(size=5, 10, 0.3) mixes keywordsize=5followed by positionals, causing SyntaxError. numpy.random.binomial(5, 10, 0.3) swaps parameters incorrectly.Final Answer:
numpy.random.binomial(10, 0.3, size=5) -> Option BQuick Check:
binomial(n, p, size) = numpy.random.binomial(10, 0.3, size=5) [OK]
- Mixing argument order
- Omitting size keyword
- Using wrong function name
import numpy as np np.random.seed(0) samples = np.random.normal(loc=5, scale=2, size=3) print(np.round(samples, 2))
Solution
Step 1: Understand the code
The code sets a random seed for reproducibility, then generates 3 samples from a normal distribution with mean 5 and standard deviation 2.Step 2: Run or recall the output
Using seed 0, the samples generated are [8.53, 5.8, 6.96] after rounding to 2 decimals.Final Answer:
[8.53 5.8 6.96] -> Option DQuick Check:
Seed 0 normal samples = [8.53 5.8 6.96] [OK]
- Ignoring seed causes different outputs
- Rounding incorrectly
- Mixing mean and scale parameters
import numpy as np samples = np.random.uniform(low=0, high=1, size=4) print(samples)
Solution
Step 1: Review the numpy uniform function syntax
np.random.uniform(low, high, size)is the correct way to generate samples from a uniform distribution.Step 2: Check the code correctness
The code uses correct function name, parameter order, and size as integer for 1D array. It will run without error and print 4 random values between 0 and 1.Final Answer:
There is no error; the code runs correctly. -> Option AQuick Check:
Uniform sampling syntax correct = There is no error; the code runs correctly. [OK]
- Confusing numpy.random.uniform with Python's random.uniform
- Swapping low and high values
- Using tuple for size unnecessarily
Solution
Step 1: Understand the problem
We want to simulate rolling a die 1000 times, each roll equally likely from 1 to 6, then count how many times 4 appears.Step 2: Evaluate each option
rolls = np.random.randint(1, 7, size=1000) count_4 = np.sum(rolls == 4):rolls = np.random.randint(1, 7, size=1000) count_4 = np.sum(rolls == 4)uses randint from 1 to 6 (exclusive upper bound 7), perfect for die rolls. rolls = np.random.binomial(6, 1/6, size=1000) count_4 = np.count_nonzero(rolls == 4):rolls = np.random.binomial(6, 1/6, size=1000) count_4 = np.count_nonzero(rolls == 4)uses binomial which models number of successes in trials, not die rolls. rolls = np.random.choice([1,2,3,4,5,6], size=1000) count_4 = np.sum(rolls == 4):rolls = np.random.choice([1,2,3,4,5,6], size=1000) count_4 = np.sum(rolls == 4)omits replace=True (defaults to False), raising ValueError since size=1000 > 6. rolls = np.random.normal(4, 1, size=1000) count_4 = np.sum(rolls == 4):rolls = np.random.normal(4, 1, size=1000) count_4 = np.sum(rolls == 4)uses normal distribution, inappropriate for discrete die rolls.Step 3: Choose the best correct code
rolls = np.random.randint(1, 7, size=1000) count_4 = np.sum(rolls == 4)is the correct way to simulate die rolls and count 4s.Final Answer:
rolls = np.random.randint(1, 7, size=1000) count_4 = np.sum(rolls == 4) -> Option CQuick Check:
Use randint for discrete uniform die rolls = rolls = np.random.randint(1, 7, size=1000) count_4 = np.sum(rolls == 4) [OK]
- Using binomial instead of uniform discrete
- Using normal distribution for discrete outcomes
- Forgetting replace=True in choice
Ready to prove it?
Pass the test and this pattern moves unknown
