docs: def randomly_assign_instances(semester, pool, profiles=None, instances=None):
      """
      Randomly assigns workshift instances to profiles.
  
      Returns
      -------
      list of workshift.WorkshiftProfile
      list of workshift.WorkshiftInstance
      """
      if profiles is None:
          profiles = WorkshiftProfile.objects.filter(semester=semester)
      if instances is None:
          instances = WorkshiftInstance.objects.filter(
              Q(info__pool=pool) |
              Q(weekly_workshift__pool=pool),
              workshifter__isnull=True,
              closed=False,
          ).exclude(
              weekly_workshift__workshift_type__assignment=WorkshiftType.NO_ASSIGN,
          )
  
      instances = list(instances)
      profiles = list(profiles)
  
      # List of hours assigned to each profile
      hours_mapping = defaultdict(float)
      total_hours_owed = defaultdict(float)
  
      semester_weeks = (semester.end_date - semester.start_date).days / 7
  
      # Initialize with already-assigned instances
      for profile in profiles:
          for shift in profile.instance_workshifter.filter(
                  Q(info__pool=pool) |
                  Q(weekly_workshift__pool=pool)
          ):
              hours_mapping[profile] += float(shift.hours)
          pool_hours = profile.pool_hours.get(pool=pool)
          if pool.weeks_per_period == 0:
              total_hours_owed[profile] = pool_hours.hours
          else:
              periods = semester_weeks / pool.weeks_per_period
              total_hours_owed[profile] = periods * float(pool_hours.hours)
  
      while profiles and instances:
          for profile in profiles[:]:
              instance = random.choice(instances)
              instance.workshifter = profile
              instance.save(update_fields=["workshifter"])
  
              instance.logs.add(
                  ShiftLogEntry.objects.create(
                      person=instance.workshifter,
                      entry_type=ShiftLogEntry.ASSIGNED,
                      note="Randomly assigned.",
                  )
              )
  
              instances.remove(instance)
  
              hours_mapping[profile] += float(instance.hours)
              if hours_mapping[profile] >= total_hours_owed[profile]:
                  profiles.remove(profile)
              if not instances:
                  break
  
      return profiles, instances