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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.