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 uniform distribution..
Uniform distribution is a type of probability distribution in which all the numeric variables have an equal probability to occur. The are the most popular type of distribution in generating random numbers.
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 runif() function to carry out this task.
Syntax: runif(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 uniform_dist = runif(100, min = 1, max = 30) #plotting a histogram of the generated numbers using hist() function hist(uniform_dist, breaks = 3)