What is the use of runif function in R

This recipe explains what is the use of runif function in R

Recipe Objective

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: ​

  1. Uniform distribution
  2. Normal distribultion

These random numbers generated mimic the properties of uniform or normal distribution in a certain interval. ​

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. ​

runif() function generates random numbers from uniform distribution. ​

In this recipe, you will learn how to generate a random uniform distribution using runif. ​

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. ​

​Should I Learn Python or R for Data Science? Unlock the Answer 

Example: Generating 100 random numbers from a uniform distribution by seeting a seed

We use runif() function to carry out this task. ​

Syntax: runif(n, min = , max = ) ​

where: ​

  1. n = size of the distribution
  2. min, max = specifies the interval in which you would like the distribution to be

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) round(uniform_dist)

26 23 9 16 29 29 4 3 11 12 22 23 1 23 7 14 10 4 9 25 15 2 14 3 9 3 27 30 3 21 11 14 25 6 16 15 15 27 20 8 18 2 14 15 23 14 20 23 13 20 3 15 9 29 5 16 1 14 9 2 13 4 28 2 28 2 26 18 5 18 2 13 6 18 27 12 17 15 20 9 7 27 3 28 22 14 11 16 5 2 16 10 25 29 25 26 26 23 11 2

Note: ​

  1. The distribution remains constant even after multiple execution.
  2. You can see that the mean, mode and median co-incides in the above plot indicating a normal distribution

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.