Results 1 to 5 of 5

Thread: random not working?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 1999
    Location
    France
    Posts
    90

    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

  2. #2
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    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!

  3. #3
    Guest
    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.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 1999
    Location
    France
    Posts
    90

    Thumbs up

    Thanks, I'll give it a shot.

  5. #5
    Lively Member
    Join Date
    Jan 2000
    Posts
    123
    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
  •  



Click Here to Expand Forum to Full Width