Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:23:50+00:00 2026-06-16T04:23:50+00:00

I will assign 8 days off to a crew randomly in a calendar month.

  • 0

I will assign 8 days off to a crew randomly in a calendar month.

I would like to randomly choose 8 days, and the days off distribution should be as even as possible. I mean all 8 days-off shouldn’t be gathered in first 8 days of the month, for example.

For example: [1, 5, 8, 14, 18, 24, 27, 30] is a good distribution.
[1,2,3,4,26,27,28,29] is not a good distribution.

Actually, a crew can’t work 7 consecutive days. In every 7 days, there must be 1 day-off.

All days are treated equally, ie Sundays are not days-off by themselves. Crew may work on weekends as well.

I want to choose days-off one by one. Not 8 of them together at once.

Could you recommend an algorithm using python to achieve this?

Not all days in the month may be available to be days off.

Best Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:23:51+00:00Added an answer on June 16, 2026 at 4:23 am

    This is the key here:

    Actually, a crew can't work 7 consecutive days. In every 7 days, there must be 1 day-off.

    Reword the problem to say a random 2 days in every 7 days (or divide the month into four lengths of time as appropriate). You are then guaranteed an even-ish distribution. Use random.sample() as Martijn Pieters suggests.

    You can generate two values using this technique from the first week, then yield them in sequence if you want them one by one.

    edit:

    As observed by tcaswell, there are still some cases where you end up with ten days in a row on duty. To combat this, you can assign a day off every three days, create a list of ten, and remove two days at random from the subset of days that don’t invalidate the 7-continuous-day criteria.

    Alternatively, you could just keep generating lists using the original algorithm until it fits the criteria, since you’re very likely to get a valid solution anyway. You’d have to write a validation function of some kind, but it would be very easy to do since you’re just counting the longest continuous string of days on.

    CODE:

    An implementation of the second option.

    import random
    from itertools import chain
    from itertools import count
    
    def candidate(m):
        ''' Returns 2 days per week, in m days, where m is the length of the month. '''
        weeks = weeksmaker(m)
        return sorted(list(chain(*[random.sample(week, 2) for week in weeks])))
    
    def weeksmaker(m):
        ''' Divides a month up into four weeks, randomly assigning extra days to weeks. '''
        weeks = [range(i, i+7) for i in xrange(1,29,7)]
        for i in range(m - 28):
            weeks[random.randint(1, len(weeks))-1].append(i)
        c = count(1)
        return [[c.next() for day in week] for week in weeks]
    
    def valid(days, c):
        ''' Validity check. Cant work more than c consecutive days. '''
        for i in xrange(1, len(days)):
            if days[i] - days[i-1] > c:
                return False
        else:
            return True
    
    def daysoff(m, n, c):
        ''' In month length m, need n days off, cant work more than c consecutive days. '''
        while True:
            days = candidate(n)
            if valid(days, c):
                return days
    
    >>> for i in range(28, 32):
    ...     daysoff(i, 8, 7)
    ... 
    [6, 7, 10, 14, 18, 20, 27, 28]
    [4, 7, 10, 13, 19, 21, 23, 24]
    [2, 4, 9, 13, 15, 20, 25, 27]
    [1, 3, 9, 12, 18, 19, 24, 28]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UIView whose layers will have sublayers. I'd like to assign delegates
Play will assign the parameters from request to action parameters, like: public static void
I'm trying to assign weeks from 1 - 5 for days in a month
I would like to assign string {MY_URL, MY} to string variable. So in my
Ok guys so a little background... I'm writing a small application that will assign
I need to assign a variable which will be used to create the id
I want to assign categories based upon the format of the book that will
I have some jquery which will calculate the items in my menu and assign
Will the element be visible even for a blink of an eye If it
Possible Duplicate: Spawn a background process in Ruby Spent a couple days poking at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.