|
-
Jun 29th, 2000, 07:24 AM
#1
Thread Starter
Hyperactive Member
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
-
Jun 29th, 2000, 08:57 AM
#2
Hyperactive Member
This may help 
Code:
' I assume you have valid reason for these
' otherwise, they belong in your sub.
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()
Dim res As Integer, chk As Boolean
' generate random value from one of 11 possible between 24 and 360
Do
' focus randomness to most discrete range
res = Rnd * 15
Select Case res
Case 1 To 6, 8 To 10, 12, 15: chk = True
' ignore non-applicable values (0, 7, 11, 13, 14)
Case Else: chk = False
End Select
Loop Until chk
' Do kind CPU division
rstudents = 360 \ res
' Show number of students in Label1
Label1.Caption = rstudents
' generate random number of projects between 4 and 6
res = Rnd * 2
' FWIW, Rnd is a bit of a stone-age throwback.
' It doesn't play well with nested calculations,
' so use a var/param instead.
rprojects = res + 4
' Show number of projects in Label2
Label2.Caption = rprojects
' Stack deck in your favor, leave room for trailing values of at least 3
' (this might better go into a function of its own)
' rfirst is random number between 3 and (rstudents - (3 * (rprojects - 1)))
res = rstudents - (3 * (rprojects - 1))
res = Rnd * res
If res < 3 Then rfirst = 3 Else rfirst = res
' rsecond is random number between 3 and (rstudents - (3 * (rprojects - 2)) - rfirst)
res = rstudents - (3 * (rprojects - 2)) - rfirst
res = Rnd * res
If res < 3 Then rsecond = 3 Else rsecond = res
' rthird is random number between 3 and (rstudents - (3 * (rprojects - 3)) - rfirst - rsecond )
res = rstudents - (3 * (rprojects - 3)) - rfirst - rsecond
res = Rnd * res
If res < 3 Then rthird = 3 Else rthird = res
' rfour is remainder of results minus rfirst and rsecond and rthird
rfour = rstudents - rfirst - rsecond - rthird
If rprojects = 4 Then
rfourth = rfour
rfifth = 0: rfive = 0: rsixth = 0
Else
' rfourth is random number between 3 and yada, yada
res = rstudents - (3 * (rprojects - 4)) - rfirst - rsecond - rthird
res = Rnd * res
If res < 3 Then rfourth = 3 Else rfourth = res
' rfive is remainder of results minus
' rfirst and rsecond and rthird and rfourth
rfive = rstudents - rfirst - rsecond - rthird - rfourth
If rprojects = 5 Then
rfifth = rfive: rsixth = 0
Else
' set rfifth
res = rstudents - (3 * (rprojects - 4)) - rfirst - rsecond - rthird - rfourth
res = Rnd * res
If res < 3 Then rfifth = 3 Else rfifth = res
' set rsixth
rsixth = rfive - rfifth
End If
End If
Picture1.Print rfirst & " " & rsecond & " " & rthird & " " & rfourth & _
IIf(rfifth > 0, " " & rfifth & IIf(rsixth > 0, " " & rsixth, ""), "")
End Sub
Please polish this up, since I winged it.
[Edited by Mongo on 06-30-2000 at 01:27 AM]
-
Jun 30th, 2000, 04:57 AM
#3
Thread Starter
Hyperactive Member
Mongo,
Thanks. I've learnt a lot from your code. I didn't understand the beginning at first - finding a number that divides evenly into 360, but now I see that you go through all the factors, choose the ones that divide evenly into 360, pick one of these at random and divide it into 360 - those returning another factor. Cool.
When you say 'I presume you have valid reasons for these, otherwise they belong in your sub', what do you mean?
Is this the difference between Private Sub and Public Sub?
Private sub number as integer
'number' can only be accessed through code on the same form.
Public sub number as integer
'number' can be accessed throughout the entire project - from modules, other forms etc....
What difference does it make if Public Sub is declared at the top, outside the Sub,like I have it - or between the Private Sub and End Sub?
-
Jun 30th, 2000, 07:23 AM
#4
Fanatic Member
By "valid reason" he means that since you have made the variables public to the whole form or module or whatever, that you want to read the variables from other subs and functions too.
If the only the sub using them is this sub then they should be Dim'ed inside the sub so that their memory will be released to the system after the sub returns and so you're not treading on your own namespace. You may want to use these Variable names again in another sub but with different values but here you'd be overwriting the origonal values rahter than creating new ones.
In large projects if all variable are public, it becomes very confusing to write new modules for and a nightmare to support -even for the origonal author- not to mention the memory waste
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
Jun 30th, 2000, 07:32 AM
#5
Hyperactive Member
In a unrelated topic, Paul do you watch the Crocidle Hunter on television?
He is awesome.
-
Jun 30th, 2000, 12:30 PM
#6
Hyperactive Member
Paul282 explained what I meant well.
-
Jun 30th, 2000, 11:32 PM
#7
Fanatic Member
Originally posted by billrogers
In a unrelated topic, Paul do you watch the Crocidle Hunter on television?
He is awesome.
???????
never heard of it!
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|