+ Reply to Thread
Results 1 to 6 of 6

Randbetween function with restricted conditions

  1. #1
    Registered User
    Join Date
    05-25-2018
    Location
    Holland
    MS-Off Ver
    Excel 2016
    Posts
    3

    Randbetween function with restricted conditions

    Hey Excel community,

    I have come accross a problem that I cannot overcome and I am wondering whether a macro is the only way to solve this problem. I hope not since I don't know much about macros.

    I am trying to make an automatic job schedule with the following availability options from different persons:

    excel picture1.PNG

    I came this far as that I select all the ''yes'' options per date and pick a randomized person that is able to work on that date. To create some equality between the employees I want to 'force' the randbetween to atleast schedule a person once per two weeks and a maximum number of day parts which they can fill in themselves (see row 29). However the randbetween is not as simple as picking a number between 1 and the maximum number of every employee as you can see in the following image, because the other randbetween already has to figure out which random person has to work on a specific day. How can I restrict the randbetween in this formula so that the sum over these two weeks is between 1 and max amount chosen by employee? Someone knows how to adjust the formula?

    excel picture2.PNG

    Thankyou very much. Preciate it.
    Attached Files Attached Files
    Last edited by Simeonb; 05-25-2018 at 05:04 AM. Reason: Added xls file

  2. #2
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Randbetween function with restricted conditions

    Welcome to the forum!

    RANDBETWEEN doesn't work well with these sorts of restrictions, since it doesn't have the "intelligence" to plan ahead to make sure everybody gets a chance. I've been poking at this for a few days, though, and I think I have a solution that might be as close as you can get. For each day, the formula below looks at who is available, what their maximums are, how many days they've worked already, and how many days of availability they have left. In short, it selects the available person with the smallest window of available:maximum days remaining, without going over the max. When there is a tie for a particular day, it randomly chooses between the tied people - with your current data, the random factor only comes into play for about 4 or 5 days.

    As I said, however, it's not perfect. The availability can be poorly stacked, or the random choices could make someone necessary unavailable at the end of the month - in your attachment, person 3 is the only available person for the last day, and is only under the max at that point in half of my simulations. Fortunately, all you need to do is to hit the F9 key to re-randomize the RANDBETWEENs until you get a distribution that fills every day.

    Ultimately, it's not perfectly random, but it uses an element of randomness while still attempting to account for everyone's restrictions.

    The formula below goes in B34. It should be array-confirmed (Ctrl + Shift + Enter instead of just Enter):

    =IFERROR(INDEX($G34:$O34,1,INDEX(SMALL(IF(IF((LEN($G34:$O34)>0)*($B$29:$J$29-COUNTIF($B$33:$B33,$G$32:$O$32)>0),(COUNTIF($G34:$O$59,$G34:$O34)-($B$29:$J$29-COUNTIF($B$33:$B33,$G$32:$O$32))))=SMALL(IF((LEN($G34:$O34)>0)*($B$29:$J$29-COUNTIF($B$33:$B33,$G$32:$O$32)>0),(COUNTIF($G34:$O$59,$G34:$O34)-($B$29:$J$29-COUNTIF($B$33:$B33,$G$32:$O$32)))),1),COLUMN($G34:$O34)-COLUMN($G34)+1),{1,2,3,4,5,6,7,8,9}),1,RANDBETWEEN(1,SUMPRODUCT(--ISNUMBER(SMALL(IF(IF((LEN($G34:$O34)>0)*($B$29:$J$29-COUNTIF($B$33:$B33,$G$32:$O$32)>0),(COUNTIF($G34:$O$59,$G34:$O34)-($B$29:$J$29-COUNTIF($B$33:$B33,$G$32:$O$32))))=SMALL(IF((LEN($G34:$O34)>0)*($B$29:$J$29-COUNTIF($B$33:$B33,$G$32:$O$32)>0),(COUNTIF($G34:$O$59,$G34:$O34)-($B$29:$J$29-COUNTIF($B$33:$B33,$G$32:$O$32)))),1),COLUMN($G34:$O34)-COLUMN($G34)+1),{1,2,3,4,5,6,7,8,9})))))),"")

    To make things easier, I've added the following formula in C34 to identify when re-randomization needs to be done. If any cell says "PROBLEM, hit F9 key", hit F9 to recalculate/rerandomize until everything is good.

    =IF(F34=0,"Nobody Available",IF(B34="","PROBLEM, hit F9 key","Good"))

    Feel free to experiment with the attachment to see if it'll do:
    Attached Files Attached Files
    If your problem has been solved, please use "Thread Tools" to mark the thread as "Solved".

    If you're grateful for this site's existence and would like to contribute to the community, please consider posting something hilarious in our joke thread:
    https://www.excelforum.com/the-water...ke-thread.html

  3. #3
    Registered User
    Join Date
    05-25-2018
    Location
    Holland
    MS-Off Ver
    Excel 2016
    Posts
    3

    Re: Randbetween function with restricted conditions

    CAntosh, amazing!

    It's not flawless, but I can tell you put a lot of effort in it and I appreciate it very much.

    Maximum day parts confuses the formula. Some rows 'refuse' to randomise. If I change maximum day parts it starts randomising again.

    I'll try work a around a few minor problems but this is very much usable. Thanks sir

  4. #4
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Randbetween function with restricted conditions

    I'm glad I could help!

    By design, the maximum day element will often prevent true randomization, since the formula first looks to see which of the available workers has the fewest opportunities remaining to fit their days in. It only randomizes when there is a tie in those results. In the attachment, I think the randomization only figures in with about 4 days. Removing the maximum days criteria from the formula will offer more randomization, but it's far less likely to properly fill the days according to everyone's criteria; on the last few days the people available will all be exceeding their maximums. If you've got a lot of people available for each day, then you can probably get away with removing the max. days clauses and having more true randomization. Based on the sample, though, I had a hard time getting those last days filled without including the max clause, which attempts to look forward at what opportunities remain. Based on your real data, you'll have to figure out which balance works best for you. Good luck!

  5. #5
    Registered User
    Join Date
    05-25-2018
    Location
    Holland
    MS-Off Ver
    Excel 2016
    Posts
    3

    Re: Randbetween function with restricted conditions

    Sorry to bother again. I am not sure if you will get a notification of this message but it would be nice if you did. The project has stood still and today I have tried to remove the Max clause and just stick with a minimum of 1 day part per person so I can just fairly press F9 until I get the desired result. I have tried to modify the formula, but since it's creepy long I am not sure how to. I do know that it will be much more simple to program a minimum of 1 per person, but I still lack the knowledge on how to do it for this project. It would be wonderful if you could have a look at it again. If it's to much work, please don't bother

    Thanks in advance

    Simeon

  6. #6
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Randbetween function with restricted conditions

    Removing one of the criteria opens up the door to more randomness. I think the formula below, array-entered (confirm with Ctrl + Shift + Enter) in B34 and filled down, will do the trick:

    =INDEX($G34:$O34,1,IF(SUMPRODUCT((LEN($G34:$O34)>0)*(COUNTIF($G34:$O$59,$G34:$O34)=1)*(COUNTIF($B$33:$B33,$G34:$O34)=0))>0,LARGE(IF((LEN($G34:$O34)>0)*(COUNTIF($G34:$O$59,$G34:$O34)=1)*(COUNTIF($B$33:$B33,$G34:$O34)=0),COLUMN($G34:$O34)-COLUMN($G34)+1),1),INDEX(SMALL(IF(LEN($G34:$O34)>0,COLUMN($G34:$O34)-COLUMN($G34)+1),{1,2,3,4,5,6,7,8,9}),1,RANDBETWEEN(1,SUMPRODUCT(--ISNUMBER(SMALL(IF(LEN($G34:$O34)>0,COLUMN($G34:$O34)-COLUMN($G34)+1),{1,2,3,4,5,6,7,8,9})))))))

    In short, if one of the options has not been chosen yet and this is his/her last available day, then he/she is assigned. Otherwise, a random selection from the available candidates will be made. Take a look at the attachment to see if it's working as desired:
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. RANDBETWEEN Function with conditions
    By Jumpingbeans_8 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-26-2020, 05:38 PM
  2. Replies: 10
    Last Post: 07-03-2015, 04:29 PM
  3. If and Randbetween Function
    By dr_phd1 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-19-2013, 04:02 PM
  4. RANDBETWEEN function capped
    By derek594 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-30-2013, 11:14 PM
  5. Randbetween Function
    By CV_GAS in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-24-2008, 03:45 PM
  6. Freezing the RANDBETWEEN function
    By zuri125 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-11-2008, 01:58 AM
  7. randbetween function
    By Tracey in forum Excel General
    Replies: 5
    Last Post: 02-10-2006, 08:35 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1