Today we will show how you can use the RANDBETWEEN function of Excel to get any random integer between two given numbers.
RANDBETWEEN Function in Excel (Quick View)
The above image represents a quick view of the uses of the RANDBETWEEN function. Now go through the rest part to learn about the function extensively.
Download Practice Workbook
Introduction to Excel RANDBETWEEN Function
♦ Summary:
- Returns a random integer between two given numbers, called bottom and top
- The arguments can be both integers and fractions, but the output will always be an integer.
- Includes the arguments. (If they are integers).
- Is refreshed automatically. Each time you make any change to your workbook or worksheet, the return value will be changed.
- Available from Excel 2003.
♦ Syntax:
The syntax of the RANDBETWEEN function is:
=RANDBETWEEN(bottom,top)
♦ Arguments:
Arguments | Required or Optional | Value |
---|---|---|
bottom | Required | The lowest number between which you want the random integer. It can be an integer or a fraction. |
top | Required | The highest number between which you want the random integer. It can be an integer or a fraction. |
Note: The arguments can be either integers or fractions. But whether they are integers or fractions, the output will always be an integer.
♦ Return Value:
- Returns a random integer between two given numbers. Includes the bottom and top arguments in case they are integers.
5 Examples of Using RANDBETWEEN Function in Excel
Here we will highlight 5 uses of the Excel RANDBETWEEN function with suitable examples. So, let’s start!
1. Getting Random Integers
Look at the data set below. We have the names of 10 employees of a company named Sunshine Group.
- Now we will provide each employee with an employee ID between 101 and 110. We can provide that using the RANDBETWEEN function of Excel. First, we will enter this formula into each of the cells.
=RANDBETWEEN(101,110)
Here, the RANDBETWEEN(101,110) syntax returns a random integer between 101 and 110.
See, we have inserted an ID between 101 to 110 for each of the employees.
Note: There is a problem here. We want the employee IDs to be unique, right? That means each of the 10 employees will have a unique employee ID between 101 and 110.
Using only the RANDBETWEEN function 10 times does not give any guarantee to provide 10 unique numbers. As we can see, in the image above, 106, 104, 102, and 107 have been repeated. We do not want that. To get the desired value, follow the below steps. You can use the combined formula of applying INDEX, FILTER, COUNTIF, RANDBETWEEN, ROW, and ROWS functions to get a unique employee ID for each employee randomly.
- First, fill up the first cell using a simple RANDBETWEEN function.
=RANDBETWEEN(101,110)
- Then insert this complex formula in the 2nd cell.
=INDEX(FILTER(ROW($A$101:$A$110),COUNTIF($B$5:B5,ROW($A$101:$A$110))=0),RANDBETWEEN(1,ROWS(FILTER(ROW($A$101:$A$110),COUNTIF($B$5:B5,ROW($A$101:$A$110))=0))),1)
Formula Explanation
- Here I have used ROW(A101:A110) because I want random numbers between 101 and 110. You use it according to your needs. For example, if you want random numbers between 101 to 200, use ROW(A101:A200) or ROW(B101:B200), and so on.
- B5 cell within the COUNTIF function is the first cell reference where I have inserted a number manually using a simple RANDBETWEEN You use your one.
- Then double-click on the Fill Handle. You will find all the cells filled with unique random numbers from 101 to 110, like this:
Note: The FILTER function is available in only Office 365. So, you cannot use this formula unless you are in Office 365.
Read More: 51 Mostly Used Math and Trig Functions in Excel
2. Getting Random Dates
You can also get any random date between two dates using the RANDBETWEEN function. Just put two dates in place of the bottom and top arguments instead of numbers.
- Look at the data set below. We have the names of 10 candidates selected for an interview in the Sunshine Group.
- Now we will enter random dates for their interviews in September 2021. That means the dates need to be between 1-September-2021 and 30-September-2021.
The formula will be:
=RANDBETWEEN(DATE(2021,9,1),DATE(2021,9,30))
Formula Explanation
- DATE(2021,9,1) and DATE(2021,9,30) return the bottom and top values respectively, on 1-Sep-2021 and 30-Sep-2021. See the DATE function for details.
- RANDBETWEEN(DATE(2021,9,1),DATE(2021,9,30)) returns a random date between these two dates including these two.
See, we have got a random date for each of the candidates.
Note: Here, we have the same problem as above. Some dates have been repeated. Like 12-Sep-2021 and 17-Sep-2021.
We do not want that. We want a unique date for each of the candidates. How to do that?
No worries! We will accomplish this using a combination of Excel’s INDEX, FILTER, COUNTIF, RANDBETWEEN, SEQUENCE, ROWS, and DATE functions. Remember, the SEQUENCE function is only available in Office 365. Follow the below steps.
- First, fill up the first cell with a simple RANDBETWEEN function.
=RANDBETWEEN(DATE(2021,9,1),DATE(2021,9,30))
- Then insert this complex formula in the 2nd cell.
=INDEX(FILTER(DATE(2021,9,SEQUENCE(30,1,1,1)),COUNTIF($C$4:C4,DATE(2021,9,SEQUENCE(30,1,1,1)))=0),RANDBETWEEN(1,ROWS(FILTER(DATE(2021,9,SEQUENCE(30,1,1,1)),COUNTIF($C$4:C4,DATE(2021,9,SEQUENCE(30,1,1,1)))=0))),1)
Formula Explanation
- Within SEQUENCE(30,1,1,1), 30 is the total number of dates within the interval. Here we have 1-sep to 30-Sep, a total of 30 days. You use your one.
- The third argument 1 within SEQUENCE(30,1,1,1) denotes the starting day of your interval. For example, if you start on 15 September, use SEQUENCE(30,1,15,1).
- Within the DATE function, 2021 and 9 denote the year and the month of the starting date respectively. For example, if you start in February 2022, use DATE(2022,2,…)
- C5 within the COUNTIF function denotes the first cell reference where I inserted a date manually. You use your one.
- Then double-click on the Fill Handle. You will see all the cells filled with unique dates from 1-Sep-2021 to 30-Sep-2021.
Read More: 44 Mathematical Functions in Excel (Download Free PDF)
Similar Readings
- How to Use SUMIF Function in Excel (7 Suitable Examples)
- Use ROUND Function in Excel (9 Examples)
- How to Use Excel SUMPRODUCT Function (7 Suitable Examples)
- Use MMULT Function in Excel (6 Examples)
- How to Use TAN Function in Excel (6 Examples)
3. Selecting Random Data from a Table
Now another case. Let us have a data set of the names and joining dates of 10 employees of the Sunshine Group.
Now the chief of the company has decided to throw a surprise party on the joining date of any of the employees randomly.
How can we select one randomly?
Easy! Just use this formula:
=INDEX(B5:C14,RANDBETWEEN(1,10),2)
Formula Explanation
- RANDBETWEEN(1,10) returns a random integer between 1 to 10. Here we have used 1 to 10 because we have a total of 10 employees.
- INDEX(B5:C14,RANDBETWEEN(1,10),2) returns the cell content of the cell with the random row number and column number 2 (“Joining Date“) from the range B5:C14.
- See, we have selected a date randomly from the table.
Read More: How to Use RAND Function in Excel (5 Examples)
4. Dividing into Groups Using the RANDBETWEEN Function
Now, look at the data set below. We have a list of 12 countries qualified for the upcoming ICC Cricket World Cup in 2023.
- Initially, we will divide the teams into four groups, A, B, C, and D. You can use the CHOOSE function of Excel in combination with the RANDBETWEEN function to accomplish this.
=CHOOSE(RANDBETWEEN(1,4),"A","B","C","D")
Formula Explanation
- RANDBETWEEN(1,4) returns an integer between 1 and 4. We have taken 1 to 4 because there are 4 groups.
- CHOOSE(RANDBETWEEN(1,4),”A”,”B”,”C”,”D”) assigns one group to each of the teams based on the result of the RANDBETWEEN function.
See, we divided the teams into four groups.
But if you notice carefully, you will find there is a problem too. We want all the groups to have an equal number of teams, 3 in this case. But here we have got 4 teams in group D and 2 teams in group A. We do not want this. But no worries. We will solve this too. Follow the below steps.
- First, fill up the first cell randomly with the CHOOSE function.
- Then insert this complex formula in the 2nd (C6) cell.
=INDEX(FILTER({"A","B","C","D"},COUNTIF($C$5:C5,{"A","B","C","D"})<3),1,RANDBETWEEN(1,COLUMNS(FILTER({"A","B","C","D"},COUNTIF($C$5:C5,{"A","B","C","D"})<3))))
Formula Explanation
- {“A”,”B”,”C”,”D”} are the names of my groups. You use anything according to your wish.
- I have used COUNTIF($C$5:C5,{“A”,”B”,”C”,”D”})<3) because I want each group to have 3 teams. You use it according to your needs.
- Within the COUNTIF function, C5 is the cell reference of the first cell where I inserted the group manually. You use your one.
- Then double-click on the Fill Handle. You will get all the cells filled with the groups, each group having 3 teams.
- Finally, you can use this formula anywhere other than Office 365 because the FILTER function is only available in Office 365.
5. Using RANDBETWEEN Function for Decimals
The RANDBETWEEN function returns integer outputs only. But you can combine it with the RAND function to generate random decimal numbers between two specified numbers.
- Apply the following formula in cell C5 and drag the Fill Handle icon below.
=RANDBETWEEN(101,110-1)+RAND()
- You need to subtract 1 from the top argument to keep the generated number within the specified limits.
Limitations of Excel RANDBETWEEN Function
The output of the RANDBETWEEN function changes each time any other cell in the workbook or worksheet is changed. It may be any cell connected to the function or not.
- To solve this problem, you have to turn the RANDBETWEEN outputs into normal text values before going to another task.
- To do this, first, select all the RANDBETWEEN
- Then press Ctrl + C on your keyboard.
- And then right-click on your mouse. From Paste Options, select
- You will find all the RANDBETWEEN outputs converted into text values.
Common Errors with Excel RANDBETWEEN Function
Error | When They Show |
---|---|
#NUM! | Shows when the bottom argument is less than the top argument. |
#VALUE! | Shows when any argument is of the wrong data type. For example, when the top or bottom arguments are text values in place of numbers. |
Conclusion
Using these methods, you can use the RANDBETWEEN function of Excel to get any random integer between two numbers when necessary. Do you have any questions? Feel free to inform us. Please visit our ExcelDemy blog to explore more about Excel. Stay with us and keep learning.
Related Articles
- How to Use SUMIFS Function in Excel (6 Handy Examples)
- VBA EXP Function in Excel (5 Examples)
- How to Use MOD Function in Excel (9 Suitable Examples)
- Use INT Function in Excel (8 Suitable Examples)
- How to Turn Off Scientific Notation in Excel (5 Useful Methods)
- Use SUM Function in Excel (6 Easy Examples)
- How to Find Inverse Cosine of a Number in Excel (3 Easy Methods)