RANDBETWEEN() excluding values

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,907
Office Version
  1. 365
Platform
  1. Windows
Hi,

Does anybody know how to do a RANDBETWEEN(1,100) but have it exclude numbers that already exist within a defined range?

Thanks,
Jon
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi noj,

I don't know of any easy way to do this using RANDBETWEEN, so here is a custom user-defined function to make it easy:

Function RandBetweenInt(Lowest As Long, Highest As Long, Exclude As Range) As Long
Dim R As Long
Dim C As Range
Do
R = Lowest + Int(Rnd() * (Highest + 1 - Lowest))
For Each C In Exclude
If R = C Then Exit For
Next C
Loop Until C Is Nothing

RandBetweenInt = R
Application.Volatile
End Function


To use this function, enter the cell formula

=RandBetweenInt(1,100,C1:C20)

where C1:C20 is the range containing the numbers you want to exclude.

To install this UDF in your workbook, go to the VBE (keyboard Alt-TMV), insert a new macro module (Alt-IM) and paste this code into the Code pane.

Damon
 
Upvote 0
Hi - I want to do a similar thing, but the numbers I want to exclude are not within a set range, but rather determined by a previous column of randomly generated numbers.

I want to generate 2 random numbers bewteen 1 and 5, with 13 repetitions. The seond number can't be the as the first.

So in Column A1 I would put =randbetween(1,5). In B1, I want to generate another random number between 1 and 5, but exclude the result in A1.

Basically, I'm trying to randomly pick TWO days of the week, weekdays only Mon-Fri. I want to do this for 13 weeks.

Any ideas?

thanks!
 
Upvote 0
Excellent Barry! It works perfectly. I can't pretend to understand the formula's components in B1, but it worked and did not replicate any numbers generated in A. Thanks!
 
Upvote 0
Whichever number you have in A1 the IF function generates an array which contains the other 4, so if A1 = 2 this part

IF(A1={1,2,3,4,5},"",{1,2,3,4,5})

generates this array {1,"",3,4,5}

Then SMALL gives you the nth smallest out of that array [where n is generated by RANDBETWEEN(1,4) ]

SMALL could be replaced by LARGE, it makes no difference which of those you use....
 
Upvote 0
Actually you can, I should have tested it before asking (and read lhousesoccer's #3 post more carefully):

=SMALL(IF(RANDBETWEEN(1,5)={1,2,3,4,5},"",{1,2,3,4,5}),RANDBETWEEN(1,3))

works fine. He wanted to show the extra column.

I was looking for a single randbetween type formula that excluded numbers, but this one seems to do it:

e.g.

=CHOOSE(INT(RAND()*7)+1,1,3,4,6,8,9,10)

for 1 to 10 excluding 2,5,7
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,393
Members
449,081
Latest member
JAMES KECULAH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top