|
-
Jun 26th, 2000, 05:41 PM
#1
Thread Starter
Lively Member
Hi,
I'm working a small maths program for students.
A question like this comes up:
"A group of 40 students were given 4 projects each to complete. The results are shown on the table below."
The number of students can range from 24 to 55, at random, when the button is clicked.
The number of projects can range from 4 to 6, at random, when the button is clicked.
Then the number of students is divided into 4 random numbered groups.
For example, let's say the number of students is 42. Then the groups could be: 4, 10, 0, 28...or....16,10,9, 7...etc.
But it's not working! My numbers aren't random! Every time I load my form, and click the command button, the numbers come up in same order. Could someone please look at my code and tell me what's wrong? I heard VB doesn't generate truly random numbers, but it's a bit far when it comes up the same every single time.
-------
'Required: command button, picturebox and label
Dim rstudents As Integer
Dim rprojects As Integer
Dim rfirst As Integer
Dim rsecond As Integer
Dim rresult As Integer
Dim rthird As Integer
Private Sub CmdRandom_Click()
rstudents = 24 + Int(32 * Rnd())
rprojects = 4 + Int(3 * Rnd())
Label1.Caption = "A group of " & rstudents & " students were given " & _
rprojects & " projects each to complete. The results are shown on the" & _
" table below."
rfirst = (Rnd * (rstudents))
rsecond = (Rnd * (rstudents - rfirst))
rresult = rstudents - rfirst - rsecond
rthird = (Rnd * (rresult))
rresult2 = rstudents - rfirst - rsecond - rthird
Picture1.Print rfirst & vbNewLine & rsecond & vbNewLine & rthird & vbNewLine & rresult2
End Sub
-
Jun 26th, 2000, 05:42 PM
#2
Fanatic Member
I think i see the problem.
Bung a "Randomize" statement at the start of your command buttons click event.
Iain, thats with an i by the way!
-
Jun 26th, 2000, 05:47 PM
#3
From the help:
For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previous number as a seed for the next number in the sequence.
Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.
-
Jun 26th, 2000, 06:24 PM
#4
Thread Starter
Lively Member
Thanks, I'll give it a shot.
-
Jun 27th, 2000, 05:40 AM
#5
Lively Member
I always use randomize timer... but I think they are they same thing.
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
|