Generating a random number in R is a fundamental operation for statisticians, data scientists, and programmers using the language. Whether you are running a simulation, bootstrapping a dataset, or initializing a machine learning algorithm, understanding how to control randomness is critical. The base installation of R provides several powerful functions to handle this task with precision and flexibility.
Understanding the Core Function: sample()
While many beginners look for a specific "random number" button, R typically relies on the versatile sample() function. This function allows you to draw random samples from vector elements, which inherently includes generating random integers. To generate a single random integer within a range, you would use sample(x, size), where setting x to a sequence defines the boundaries of your random selection.
Basic Syntax for Integers
To get a single random number or a vector of random numbers, the most common approach is to define a sequence and sample from it. For example, to mimic the roll of a die, you define the sides of the die as your sequence. This method is preferred over older functions like runif() when you need discrete integers rather than continuous decimal points.

| Code | Description |
|---|---|
set.seed(123)sample(1:100, 1) |
Generates a single random integer between 1 and 100. |
sample(1:10, 5, replace = TRUE) |
Generates a vector of 5 numbers between 1 and 10, allowing repeats. |
Ensuring Reproducibility with set.seed()
A crucial concept in random number generation is reproducibility. When debugging code or validating results, you often need to generate the exact same sequence of "random" numbers. This is where the set.seed() function becomes indispensable. By setting a seed value, you initialize R's random number generator to a known state, ensuring that anyone running your code will get identical results.
The choice of the seed number is arbitrary; 123 is a common example, but any integer works. Professional workflows often document the specific seed used in research papers or production scripts to guarantee transparency and verification of the computational process.
Advanced Distributions and runif()
For scenarios requiring continuous random variables, the runif() function is the standard tool. It generates random numbers from a uniform distribution, meaning every number within a specified interval has an equal chance of being selected. This is distinct from discrete integer generation and is essential for probability modeling.

You can define the minimum and maximum bounds of this interval. By default, R generates numbers between 0 and 1, but you can adjust this to fit your specific analytical needs, such as simulating data that ranges from -1 to 1 or any other specific bounds required for your project.
Performance and Best Practices
When writing efficient R code, it is important to consider how you generate random numbers. Avoid growing objects in a loop by pre-allocating vectors. Instead of calling the random function inside a loop to fill a list, generate the entire vector of random numbers in a single call. This leverages R's vectorized operations, significantly improving performance on large-scale simulations.
Additionally, be mindful of the random algorithm itself. R uses the Mersenne-Twister algorithm by default, which is robust for most applications. However, for cryptographic security or specific stochastic processes, you may need to investigate alternative generators or packages specifically designed for high-stakes randomness.






















