Random numbers are generated in quite a few cases in statistics to carry out sampling and simulation. Mostly, a data scientist is in a need of a set of random numbers which are mostly taken from two types of distribution:
These random numbers generated mimic the properties of uniform or normal distribution in a certain interval.
In this recipe, you will learn how to create a random distribution using rnorm.
Note: Whenever we are generating random numbers, you are using an algorithm that requires a seed whose function is to initialise. These numbers are actually pseudorandom numbers which can be predicted if we know the seed and the generator. Setting a seed means iniltialising a pseudorandom generator. We set a seed when we need the same output of numbers everytime you want to generate random numbers. If we don't set a seed, the generated pseudorandom numbers are different on each execution.
We use rnorm() function to carry out this task.
Syntax: rnorm(n, min = , max = )
Additionally, use set.seed() function to set a seed. We specify any integer in the function as a seed.
# setting a seed set.seed(20) # using random numbers from normal distribution between 1 and 30 random_dist = rnorm(10000, mean = 0, sd = 1) #plotting a histogram of the generated numbers using hist() function hist(random_dist, breaks = 100)