0
0
NumPydata~5 mins

Random sampling distributions in NumPy

Choose your learning style9 modes available
Introduction

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.

When you want to simulate rolling dice many times to see possible outcomes.
When you need to understand the average height of people by sampling groups randomly.
When testing how a coin toss behaves over many flips.
When estimating the chance of drawing a certain card from a deck repeatedly.
When checking how sample means vary in repeated surveys.
Syntax
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.

Examples
Picks 3 unique numbers from 1 to 5 randomly.
NumPy
import numpy as np
sample = np.random.choice([1, 2, 3, 4, 5], size=3, replace=False)
print(sample)
Generates 5 random numbers around 10 with some spread.
NumPy
import numpy as np
sample = np.random.normal(loc=10, scale=2, size=5)
print(sample)
Simulates 4 experiments of 10 coin tosses each, counting heads.
NumPy
import numpy as np
sample = np.random.binomial(n=10, p=0.5, size=4)
print(sample)
Sample Program

This program simulates rolling a die 1000 times and shows how often each face appears as a percentage.

NumPy
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}%")
OutputSuccess
Important Notes

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.

Summary

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.