Results 1 to 6 of 6

Thread: Random = Not So Random

  1. #1

    Thread Starter
    Addicted Member Smie's Avatar
    Join Date
    Jun 1999
    Location
    Columbus, OH
    Posts
    249

    Unhappy

    Ok some guys helped me with my contol problem, thanks but now that i have got that out of the way, i have a random image appear when the program opens, but it is always 35 (out of 50) then i decided to add a button to continue putting down images, just to see if it was always 35 bieng chosen, it wasnt, but there is a distinct pattern, the random code chooses 35, 26, 28, 14 .........and so on, in the same exact order every time, here is my code, why is it doing it?


    Private Sub Command1_Click()
    Random = Int(Rnd * 50)
    MsgBox "" & Random
    rw(Random).Visible = True
    rw(Random).Left = 120
    rw(Random).Top = 240
    End Sub

  2. #2
    Member
    Join Date
    May 2000
    Posts
    43

    err....

    okay i know how to stop it, but im not too sure on why it is hapening, to stop it put this code in
    Code:
    Private Sub Form_Load()
    Randomize
    End Sub
    this will seed the random numbers by using the pc's internal clock, whtaever that means
    Hope it helps
    Cease

  3. #3

    Thread Starter
    Addicted Member Smie's Avatar
    Join Date
    Jun 1999
    Location
    Columbus, OH
    Posts
    249

    thanks!

    yes that definitly helped, thanks!

  4. #4
    Member
    Join Date
    Jun 2000
    Location
    Slovenia
    Posts
    32

    Talking

    You should use Randomize statement if you need completely randomly generated numbers ench time
    you start your program.

    You just put Randomize anywhere before Rnd function and there will be no pattern in generated numbers.
    Like this:

    Private Sub Command1_Click()

    Randomize 'you can put it anywhere between Sub and
    'End Sub before Rnd function is called

    Random = Int(Rnd * 50)
    MsgBox "" & Random
    rw(Random).Visible = True
    rw(Random).Left = 120
    rw(Random).Top = 240
    End Sub

    Hope this helps,

    Celery

  5. #5
    New Member
    Join Date
    May 2000
    Location
    Auburn,AL
    Posts
    12
    What happens is that the random numbers are always generated with a seed number on which they base the number generation.
    Without the randomize statement, the initial seed number is always the same. The randomize statement uses the system timer as the default for the initial seed number (which is the number of seconds which have elapsed since midnight).
    If you prefer, you can set the seed number with the statement

    Randomize[seed]

  6. #6

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