How to create a uniform distribution in R?

This recipe helps you create a uniform distribution 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. ​

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

Example: Creating a uniform distribution by 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) #plotting a histogram of the generated numbers using hist() function hist(uniform_dist, breaks = 3)

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

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

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 .

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

Build an AI Chatbot from Scratch using Keras Sequential Model
In this NLP Project, you will learn how to build an AI Chatbot from Scratch using Keras Sequential Model.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.