Hi,
I'd appreciate if someone could help me with this because I've been at it all day. Believe me, I've tried everything I know. (Maybe I'll be more fresh in the morning - 01:30 in these parts).
A random number between 24 and 360 is generated. Another random number between 4 and 6 inclusive is also generated. Then a range of other numbers, from four to six numbers, are generated on this basis. The sum of these numbers add up to the number between 24 and 360.
For example, lets say the number generated in Label1 is 36 and Label2 is 4.
Then the numbers in Picture1 might be: 3,21,4,8. (They are generated at random but they will all add to 36.
Or lets say the number generated in Label1 is 30 and Label2 is 6. Then the 6 numbers in Picture1 might be 5,4,15,3,3,0.(Six numbers add to 30)
The problem is, I don't want zero to appear as any of the numbers in Picture1. I want the minimum figure to be 3. For example, the case immediately above might be 5,4,12,3,3,3 rather than 5,4,15,3,3,0. I don't know how to do this - I kept getting negative values and stuff. It's all to do with that damn random instruction.
Anyway, I've attached my code below and commented it clearly, so if anybody has any help, I'd be happy to get it.
Thanks.
Public rstudents As Integer
Public rprojects As Integer
Public rfirst As Integer
Public rsecond As Integer
Public rthird As Integer
Public rfourth As Integer
Public rfifth As Integer
Public rsixth As Integer
Public rfour As Integer
Public rfive As Integer
Private Sub CmdRandom_Click()
Randomize
'generate a number between 24 and 360. It will
'be a whole number, divides evenly into 360
REDO:
Do While Not IsWhole = True
DoEvents
rstudents = Int(Rnd * 360)
If rstudents < 24 Then GoTo REDO
DivideIt = 360 / rstudents
IsWhole = (DivideIt - CInt(DivideIt)) = 0
Loop
'Show the number of students in Label1
Label1.Caption = rstudents
'generate random number of projects between 4 and 6
rprojects = 4 + Int(Rnd * 3)
'Show the number of projects in Label2
Label2.Caption = rprojects
'rfirst is a random number between 0 and rstudents
rfirst = (Rnd * (rstudents))
'rsecond is a random number between the result of rstudents
'less rfirst
rsecond = (Rnd * (rstudents - rfirst))
'rthird is random number between the result of rstudents 'less the sum of rfirst and rsecond
rthird = (Rnd * (rstudents - rfirst - rsecond))
'rfourth is random number between the result of rstudents 'less the sum of rfirst and rsecond and rthird
rfourth = (Rnd * (rstudents - rfirst - rsecond - rthird))
'rfour is the remainder of results minus
'rfirst and rsecond and rthird
rfour = rstudents - rfirst - rsecond - rthird
'rfifth is random number between the result of rstudents 'less the sum of rfirst and rsecond and rthird and rfourth
rfifth = (Rnd * (rstudents - rfirst - rsecond - rthird - rfourth))
'rfive is the remainder of results minus
'rfirst and rsecond and rthird and rfourth
rfive = rstudents - rfirst - rsecond - rthird - rfourth
'rsixth is random number between the result of rstudents 'less the sum of rfirst and rsecond and rthird and rfourth and rfifth
rsixth = rstudents - rfirst - rsecond - rthird - rfourth - rfifth
If rprojects = 4 Then
Picture1.Print rfirst & " " & rsecond & " " & rthird & " " & _
" " & rfour
End If
If rprojects = 5 Then
Picture1.Print rfirst & " " & rsecond & " " & rthird & " " & _
rfourth & " " & rfive
End If
If rprojects = 6 Then
Picture1.Print rfirst & " " & rsecond & " " & rthird & " " & _
rfourth & " " & rfifth & " " & rsixth
End If
End Sub
