# Thread: Generate random numbers based on conditions

1. ## 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.

2. ## 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
Code:
```workhours = 176
workdays = 176 / 8
dayhours = 8
' 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)
Randomize
For i = 0 To workdays - 1
tot = 0

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)
Next

days(i, numtasks - 1) = 8 - tot
If days(i, numtasks - 1) < 0 Then days(i, numtasks - 1) = 0

Next
' the days array should now be assigned to the worksheet cells```
you should dimension all variables appropriately and test extensively

#### Posting Permissions

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