Results 1 to 2 of 2

Thread: Generate random numbers based on conditions

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2015

    Generate random numbers based on conditions

    Hey guys,

    I need this function or code that produces random numbers that meets some conditions.
    I asked post a similar problem in the following website Ozgrid and ExcelGura , but they were not able to help me.

    I have restructured my sheet and my question, hopefully this time we can solve this issue.
    As you can see in the attachment I have this monthly timesheet (Sheet 3) of a person of in January 2015. I have all the information I need to populate the timesheet but I want the numbers be randomly divided over the days and projects.

    From the attachment you see that I already filled the hours as an example, but I want this to be done a random fashion that generate the desired numbers that meet the following conditions:
    1. So this person works on 2 projects.
    2. He can work 176 tot in this month.
    3. A workday has 8 work hours (this can be less if someone is sick or takes holiday)
    4. Weekends they are off
    5. The sum of project 1 should be 106 hours
    6. The sum of project 2 should be 70 hours
    7. The numbers generated should be random but should meet the above conditions!!

    I hope I am clear to what I am asking.

    On a side note: In sheet1 I have this button but it does not meet the condition. This button does generate random numbers that I need, but it does not meet the conditions 2, 3, 4, 5, 6.

    thanks in advance!
    Attached Files Attached Files

  2. #2
    Join Date
    Dec 2004

    Re: Generate random numbers based on conditions

    A workday has 8 work hours (this can be less if someone is sick or takes holiday)
    how can you preallocate the hours, including sick time? surely the sick hours have to be reduced from the total after the event
    workhours = 176
    workdays = 176 / 8
    dayhours = 8
    numtasks = 4
    ReDim tasks(numtasks - 1)
    tasks(0) = 76
    tasks(1) = 44
    tasks(2) = 38
    tasks(3) = 18
    ' all the above values should be populated from the worksheet
    '  workhours (workdays * 8) would be the number of workdays in a month less weekends and holidays
    '  this would need to be caclulated separately, in a function or whatever
    ReDim days(workdays - 1, numtasks - 1)
    ReDim sumst(numtasks - 1)
    For i = 0 To workdays - 1
        tot = 0
        ReDim sums(numtasks - 1)
        For t = 0 To numtasks - 2
            s = sums(t)
            If s > 3 Then s = 3
            days(i, t) = Int(5 - s) * Rnd + 1: Debug.Print tot, i, t
            If tot + days(i, t) >= dayhours Then days(i, t) = dayhours - tot
            If sumst(t) + days(i, t) >= tasks(t) Then days(i, t) = tasks(t) - sumst(t)
            If days(i, t) < 0 Then days(i, t) = 0
            sumst(t) = sumst(t) + days(i, t)
            sums(t) = sums(t) + days(i, t)
            tot = tot + days(i, t)
        days(i, numtasks - 1) = 8 - tot
        If days(i, numtasks - 1) < 0 Then days(i, numtasks - 1) = 0
        If sums(numtasks - 1) + days(i, numtasks - 1) >= tasks(t) Then days(i, numtasks - 1) = 0
        sumst(numtasks - 1) = sumst(numtasks - 1) + days(i, numtasks - 1)
    ' the days array should now be assigned to the worksheet cells
    you should dimension all variables appropriately and test extensively
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed

Posting Permissions

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

Click Here to Expand Forum to Full Width