Random Sampling
in R
Learn to generate random samples using rnorm, runif, and sample: the building blocks of simulation and statistical computing.
What is Random Sampling?
Random sampling means drawing values from a probability distribution or a finite set so that each draw is governed by chance. In R, three core functions handle the most common scenarios:
Why Random Sampling Matters
Every simulation, bootstrap, permutation test, and Monte Carlo method depends on the ability to generate random data. Without it, modern statistics would be impossible.
rnorm(): The Normal Distribution
runif(): The Uniform Distribution
sample(): Discrete Sampling
Continuous vs Discrete Sampling
| Continuous | Discrete | |
|---|---|---|
| Functions | rnorm(), runif() | sample() |
| Output type | Numeric (decimals) | Elements from a set |
| Values | Infinite possibilities in range | Finite set of choices |
| Example | rnorm(5) → 0.32, -1.07... |
sample(1:6, 2) → 4, 1 |
| Use case | Measurements, probabilities | Dice, cards, survey picks |
Reproducibility with set.seed()
R's random number generator is actually pseudo-random: it uses an algorithm seeded by an initial value. Setting the seed guarantees the same sequence every time.
How to Generate Random Samples
set.seed() with any integer for reproducibility.rnorm(): \( n \), \( \mu \), \( \sigma \). For runif(): \( n \), min, max. For sample(): vector, size, replace.x <- rnorm(100).hist(x), mean(x), or summary(x) to check the output.Example 1: Simulating Test Scores
Suppose test scores follow \( \mathcal{N}(75, 10^2) \). Generate 1000 scores and find the proportion above 90.
Example 2: Estimating \( \pi \) with runif()
Generate random points in a unit square and count how many fall inside the quarter-circle of radius 1. The ratio estimates \( \pi / 4 \).
Example 3: Simulating Dice Rolls
Roll two dice 5000 times and estimate \( P(\text{sum} = 7) \). The theoretical answer is \( \frac{6}{36} = \frac{1}{6} \approx 0.1667 \).
With vs Without Replacement
Weighted Sampling with prob
The prob argument lets you assign unequal probabilities to each element. R automatically normalizes the weights so they sum to 1.
Common Mistakes
replace = FALSE when you need independent draws (like dice), or TRUE when items should not repeat.rnorm() takes standard deviation (\( \sigma \)), not variance (\( \sigma^2 \)). If given \( \sigma^2 = 25 \), pass sd = 5.Summary: Three Sampling Functions
| rnorm() | runif() | sample() | |
|---|---|---|---|
| Distribution | Normal \( \mathcal{N}(\mu, \sigma^2) \) | Uniform \( [a, b] \) | Discrete (any vector) |
| Key params | mean, sd | min, max | x, size, replace |
| Output | Numeric (continuous) | Numeric (continuous) | Same type as input |
| Default | \( \mu=0,\;\sigma=1 \) | \( a=0,\;b=1 \) | Without replacement |
| Typical use | Measurement simulation | Monte Carlo, random coords | Resampling, permutations |
Question 1 of 5
Which function would you use to generate 50 random values from a normal distribution with \( \mu = 100 \) and \( \sigma = 15 \)?
sd = 15, not \( \sigma^2 = 225 \).Question 2 of 5
What does set.seed(42) do?
set.seed() initializes R's pseudo-random number generator so the same sequence of random numbers is produced each time the code runs. The argument is an arbitrary integer, not the count or range of values.Question 3 of 5
You want to simulate drawing 5 cards from a standard 52-card deck without putting them back. Which call is correct?
sample() with replace = FALSE (the default) does exactly this. runif() would give continuous decimals, not integers from a deck.Question 4 of 5
What is the range of possible values produced by runif(100, min = 3, max = 8)?
runif() generates continuous values uniformly distributed between min and max. Every value in \( [3, 8] \) is equally likely, and values never fall outside this interval: that is the defining property of the uniform distribution.Question 5 of 5
Running sample(1:3, size = 5) without specifying replace will:
replace = FALSE. You cannot draw 5 unique items from a vector of length 3: R throws the error "cannot take a sample larger than the population when 'replace = FALSE'". You must set replace = TRUE to allow repeats.