Open In App

Random Package in R

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss the random package including its use with working examples in the R programming language.

Random Package in R

The random package is created by Dirk Eddelbuettel, which allows the user to draw true random numbers by sampling from atmospheric noise via radio tuned to an unused broadcasting frequency combined with a skew correction algorithm based on the work of John von Neumann.

Syntax to install and import the random package is as follows:

install.packages("random")                
library("random")

Method 1: Create Data Set Containing Random Integers with Duplicates

In this method to create the data set containing random integers with duplicates, the user needs to first install and import the random package in the working console and then call the randomNumbers() function of this package passed with the required parameters passed to it and the data set will be created accordingly in the R programming language.

Example:

In this example, we are calling the randomNumbers function of the random package passed with the n which is the total number of random numbers to be generated, min and max parameters are passed to set the limits of the generated random number, and the cols parameters id for the number columns required in the data set and further, we are printing the data set created using the random numbers with duplicates in the R programming language.

R




dataset=randomNumbers(n = 100,          
                      min = 0,
                      max = 500,
                      col = 5)
dataset


Output:

       V1  V2  V3  V4  V5
 [1,] 365 246 245 108 141
 [2,] 146 278   8  48 438
 [3,] 220 434 429  72 369
 [4,] 272 121 282 257 210
 [5,] 213 445 322  45 453
 [6,] 355 127 145 196 166
 [7,] 138 238   8  33 227
 [8,] 437 380 477 272 416
 [9,] 242 467 445 471 156
[10,] 399 293 125  56 188
[11,] 148 384 236 130 111
[12,] 493 158 143 102 484
[13,] 400 211 199 278 134
[14,]  31 160 357 455 313
[15,] 441 366 458 251 453
[16,] 441 329  79 360 105
[17,] 365  65  41 107 362
[18,] 442 288 380 234 223
[19,] 259  15  82 193 144
[20,] 119 134 485 349   7

Method 2: Create Data Set Containing Random Sequence without Duplicates

In this method to create data set containing random sequences without duplicates, the user needs to call the randomsequence() function of the random package to get the random sequence without duplicates passed with the required parameters passed to it, and the sequence without duplicates will be created accordingly in the R programming language.

Example:

In this example, we are calling the randomsequence function passed with the min and max number which sets the limit to the random numbers generated, and further, we have passed the col parameter to the function which specifies the column’s needs and further the random sequence without duplicates as per the required parameters passed in the R programming language.

R




dataset=randomSequence(     
                      min = 5,
                      max = 40,
                      col = 4)
dataset


Output:

      V1 V2 V3 V4
 [1,] 34 19 13 25
 [2,] 12 20 15 33
 [3,]  8  6 16 18
 [4,] 21  9 39 32
 [5,] 40 22 14 24
 [6,] 23 28 35 17
 [7,] 30  7  5 29
 [8,] 37 31 38 27
 [9,] 11 26 36 10

Method 3: Create a Vector of Random Character Strings with Certain Length

Here, in this method to create a vector of random character strings with a certain length, the user needs to call the randomStrings() function from the random package passed with the required parameters accordingly to get the vector of random character strings with specified length in the R programming language.

Example:

In this example, we are calling the randomStrings() function with the as.vector() function passed with the n=10 which is the length of the vector generated, and len=5 which here is the length of the strings in the vectors further this will be generating the vector of random character strings of size 10 and the length of the string 5 in the R programming language. 

R




RandomStringVector = as.vector(randomStrings(n = 10,
                                             len = 5))
RandomStringVector


Output:

 [1] “D6T5f” “3lQBT” “ljrUM” “YzRuu” “JoJhH” “fsCCw” “291oM” “5wvjn” “ensjF” “Ijno1”



Last Updated : 02 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads