- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# How to simulate discrete uniform random variable in R?

There is no function in base R to simulate discrete uniform random variable like we have for other random variables such as Normal, Poisson, Exponential etc. but we can simulate it using rdunif function of purrr package.

The rdunif function has the following syntax −

> rdunif(n, b , a)

Here,

n = Number of random values to return

b = Maximum value of the distribution, it needs to be an integer because the distribution is discrete

a = Minimum value of the distribution, it needs to be an integer because the distribution is discrete

## Example

Let’s say you want to simulate 10 ages between 21 to 50. We can do this as follows −

> library(purrr) > rdunif(10,b=50,a=21) [1] 38 33 28 36 48 32 47 27 29 24

Now suppose we want to simulate 10 ages and the maximum age is 100 then it can be done as shown below −

> rdunif(10,100) [1] 2 80 50 50 82 68 45 72 81 25

We can also simulate discrete uniform distribution for negative integers as shown in the below example −

> rdunif(10,10,-5) [1] 3 -1 -3 3 6 5 3 5 -5 9

We can also create a table of discrete uniform random variable. Let’s say we want to generate 20 random values between -3 and +3 then it can be done as shown below −

> table(rdunif(20, 3, -3)) -3 -2 -1 0 1 3 2 4 4 3 3 4

https://www.tutorialspoint.com/what-is-the-use-of-tilde-operator-in-r

- Related Articles
- How to display central limit theorem using uniform random variable in R?
- How to generate Bernoulli random variable in R?
- How to create a binary random variable in R with given probability?
- How can I simulate an array variable in MySQL?
- How to create a table of sums of a discrete variable for two categorical variables in an R data frame?
- How to simulate normal distribution for a fixed limit in R?
- How to create histogram for discrete column in an R data frame?
- Uniform variable syntax in PHP 7
- How to repeat a random sample in R?
- How to add a variable description in R?
- How to create a dummy variable in R?
- How to create an ordinal variable in R?
- How to create a histogram for uniform data in R?
- How to generate standard normal random numbers in R?
- How to simulate target="_blank" in JavaScript ?