# Random Number Generator in R

Random Number Generator, this post will show you how to use the random package in the R programming language to generate random integers and character strings.

The fundamentals of the random Package

Real random numbers cannot be decrypted with a random seed, unlike pseudo-random numbers, which may be better in terms of security and hacker protection.

True random values are also closer to nature, which may make them more suitable for random experiments and simulation research.

## Random Number Generator

In order to use the random package’s functionality, we must first install and load the package:

install.packages("random")

After installing the package, now we can load a random library into the R console.

library("random")

### Approach 1: Make a data set with duplicates of random integers.

Will show you how to make a random data set with random integers in the first approach.

We can utilize the randomNumbers function from the random package for this assignment.

We can specify numerous arguments within this function, including the sample size, the lowest value, the maximum value, and the number of columns.

Consider the following code as an example.

random<- randomNumbers(n = 300,min = 9,max = 2000,col = 3) head(random)

V1 V2 V3 [1,] 800 1727 469 [2,] 351 1313 1177 [3,] 30 1166 1416 [4,] 934 347 1010 [5,] 1535 775 1755 [6,] 216 1865 1563

The above output, which is the RStudio console output of the previous R code, shows the first six rows of our random data.

We’ve constructed a data collection with three columns and 300 rows, as you can see.

It’s worth mentioning that the integer values in our data are duplicated. The next approach will show you how to make a data set with only one value per row.

### Approach 2: Create a data set with a random sequence and no duplicates.

We can use the randomSequence function from the random package for this.

We can define the minimum and maximum values, as well as the number of columns, within the function.

random<- randomSequence(min = 9,max = 2000,col = 3) head(random)

V1 V2 V3 [1,] 211 906 1757 [2,] 30 938 323 [3,] 1027 37 17 [4,] 474 1543 1434 [5,] 796 1450 729 [6,] 1160 702 1116

As you can see, we’ve constructed a data set with rows and three variables based on the aforementioned output. Each value only appears once.

## Approach 3: Create a vector of randomly generated character strings of a specific length.

The random package can also sample random character strings.

For testing purposes, this function generates a character vector of random strings.

Syntax:

randomStrings(N, string_size)

N: The number of random strings to generate

string_size: The number of characters in each string (default 50).

We’ll need to use the randomStrings function to accomplish this.

It’s worth noting that we’re also converting the output to a vector object rather than a matrix with the as.vector function:

random<- as.vector(randomStrings(n = 5,len = 4)) random

[1] "GQvO" "mzM6" "efMA" "PIWh" "Ox54"

In the preceding R code, a character string vector with seven vector elements was simulated. Each of these items has a three-character random character string.

Weibull Distribution in R » Quick Visualization »

Subscribe to our newsletter!