If you need a set of random numbers, e.g., for simulations, this might be the right article for you. In this article, we explain several ways to **create a vector with random numbers in R**.

We discuss the most common R functions to generate random numbers, such as random **integers**, random **numbers between 0 and 1**, and random samples **with(out) replacement**.

## How to Create a Vector with **Random Integers**

One of the most used samples in R are vectors with randomly generated integers (i.e., whole numbers). Generating such a sample is easy.

**You create a vector with random integers with the sample() function. This native R function has two obligatory arguments, namely the integers from which you want to sample, and the number of samples you want to take. A third, optional argument, tells R to sample with or without replacement. **

The first argument, the population from which R takes a sample, can have different forms. For examples:

- A continious list of integers, e.g.,
*-5:5* - A discontinious list of integers, e.g.
*c(1, 3, 5)* - A combintion of the two list above, e.g.,
*c(-5:0, 2, 4)*

The second argument, the sample size, must be a positive whole number.

In the example below, we create a sample of 10 randomly generated integers between -5 and 5. We use the *set.seed()* function so that you can replicate the outcome.

```
set.seed(123)
x <- sample(-5:5, size = 10)
x
```

As the image shows, all of the generated numbers appear exactly once. This is because we’ve generated a sample **without replacement**. However, if needed, you can use the *sample()* function to create a sample with replacement.

**You create a random sample with replacement using the replace=-option.** By default, this option is set to False. However, if you use

*, R samples with replacement, and hence your sample might contain a specific number more than once.*

**replace = TRUE**For example, in the R code below, we use the *replace=-*option to create a vector with random integers.

```
set.seed(123)
x <- sample(-5:5, size = 10, replace = TRUE)
x
```

As the outcome shows, the numbers 0 and 4 appear twice.

If you want to know how to create rounded random numbers (e.g., to 2 decimals places), please read this article.

### Sample Error

A common error while using the *sample()* function is the following:

`Error in sample.int(length(x), size, replace, prob): cannot take a sample larger than the population when 'replace = FALSE'`

This error indicates that the desired sample size is larger than the population from which you take the sample. For example, below we try to take a sample of 100 integers from a population of 11 numbers (-5, -4, -3, -2, -1, 0 , 1, 2, 3, 4, 5).

Because we sample without replacement and the sample size is larger than the population size, R returns the error.

You can solve this error by:

- Increae the population size (first argument), or
- Decrease the sample size (second argument), or
- Add the
*replace*-option to sample with replacement (third argument)

## How to Create a Vector with **Random Numbers between 0 and 1**

Another frequently used sample is a set of random numbers between 0 and 1.

**You create a vector with randomly generated numbers between 0 and 1 with the runif() function. This basic R function has three arguments:**

- A positive integer that specifies the sample size.
- The minimum of the sample population. If you want to generate a sample with numbers between 0 and 1, you should set this argument to 0.
- The maximum of the sample population. If you want to genereate a sample with numbers between 0 and 1, you should set this argument to 1.

The R code below shows how to use the *runif()* function to **create a sample of 10 observations with numbers between 0 and 1**. We use the *set.seed()* function replicability.

```
set.seed(123)
x <- runif(10, min = 0, max = 1)
x
```

Although the *runif()* is frequently used to create samples with numbers between 0 and 1, you can use this function also to generate samples with other sample populations. For example, below we create a vector of 10 elements with random numbers between -5 and 5.

```
set.seed(123)
x <- runif(10, min = -5, max = 5)
x
```

## How to Create a Vector with **Random Numbers from the Normal Distribution**

In R, you can also create a vector with a sample from a **Normal Distribution**.

**To generate numbers from a Normal Distribution, you use the rnorm() function. This function has 3 arguments, namely the sample size, and the mean and standard deviation of the normal distribution. **

In the example below, we use the *rnorm()* function to create a sample of 10 observations from the Standard Normal Distribution. That is to say, a normal distribution with a mean of 0 and a standard deviation of 1.

```
set.seed(123)
x <- rnorm(10, mean = 0, sd = 1)
x
```

We could’ve written the code above a bit shorter because we could’ve omitted the *mean*=-option and the *sd*=-option. If you don’t specify the mean and standard deviation, R automatically assumes that you want to create a sample from the standard normal distribution.

Likewise, you could also set the *mean=-*option and the *sd=-*option to other values to generate samples from another standard deviation. Below we generate 10 samples from the normal distribution with mean = 5 and standard deviation = 1.5.

```
set.seed(123)
x <- rnorm(10, mean = 5, sd = 1.5)
x
```

## How to Create a **Binary Vector**

Another type of random sample is the **binary sample**. This sample contains only 0’s and 1’s.

**You can create a binary sample with the sample() function. First, you specify the sample population (i.e., 0 and 1), followed by the sample size and the replacement argument.**

Note that, if you want to create a binary vector with more than 2 observations, you need to set the *replace=-*option to True.

Below, we create a random binary vector of 10 observations.

```
set.seed(123)
x <- sample(0:1, size = 10, replace = TRUE)
x
```

## How to Create a Vector with **Random Characters**

So far, we’ve discussed how to create vectors with random numbers. Instead of numbers, R can also generate vectors with random characters.

**You create a sample of randomly selected characters with the sample() function. The sample() function has two mandatory arguments:**

- A list of characters that serves as the sample population. For example,
*c(“A”, “B”, “C”)*. - A positive integer that specifies the sample size.

In the example below, we create a sample of 3 characters from the letter A to E.

```
set.seed(123)
x <- sample(c("A", "B", "C", "D", "E"), size = 3)
x
```

If you want to **take a sample of all 26 letters of the alphabet**, but you don’t want to write them all out as the first argument, you can use the reserved word ** LETTERS**. This reserved word contains all 26 letters of the alphabet in uppercase.

Below we use the reserved word LETTERS to generate efficiently a sample of 3 letters.

```
set.seed(123)
x <- sample(LETTERS, size = 3)
x
```

Note that, if you want to take a sample from the alphabet in lowercase, you can use the reserved word * letters*. You can also combine both reserved words to create a sample with lower- and uppercase letters (

*c(letters, LETTERS)*).

## How to Create a Vector with **Random Strings**

Above we have shown how to create a sample of random characters. If you want to generate random strings (i.e., a combination of multiple characters), then this method doesn’t work. However, there does exist an easy way to create random strings in R.

**You create random strings in R with the stri_rand_strings function from the stringi packages. To create the strings, you must provide two arguments:**

- A positive integer that indicates the number of strings you want to generate.
- A positive integer that indicates the length of each string.

In the example below, we use the *sitri_rand_strings()* function to generate 3 random strings of length 5.

```
if (!require("stringi")) install.packages("stringi")
set.seed(123)
x <- stri_rand_strings(3, 5)
x
```

As you can see, the *stri_rand_strings()* function generates strings that consist of letters (lower- and uppercase) and numbers. You can **use the pattern=-option to specify the characters that can be part of the strings**.

In the example below, we use the *pattern=-*option to generate strings that only contain letters (both lower- and uppercase).

```
if (!require("stringi")) install.packages("stringi")
set.seed(123)
x <- stri_rand_strings(3, 5, pattern = "[A-Za-z]")
x
```