Tutorial by Examples

To generate random permutation of 5 numbers: sample(5) # [1] 4 5 3 1 2 To generate random permutation of any vector: sample(10:15) # [1] 11 15 12 10 14 13 One could also use the package pracma randperm(a, k) # Generates one random permutation of k of the elements a, if a is a vector, # ...
When expecting someone to reproduce an R code that has random elements in it, the set.seed() function becomes very handy. For example, these two lines will always produce different output (because that is the whole point of random number generators): > sample(1:10,5) [1] 6 9 2 7 10 >...
Below are examples of generating 5 random numbers using various probability distributions. Uniform distribution between 0 and 10 runif(5, min=0, max=10) [1] 2.1724399 8.9209930 6.1969249 9.3303321 2.4054102 Normal distribution with 0 mean and standard deviation of 1 rnorm(5, mean=0, sd=1) [1...

Page 1 of 1