Results 1 to 11 of 11

Thread: Random Numbers

  1. #1

    Thread Starter
    Hyperactive Member Arrow_Raider's Avatar
    Join Date
    Dec 2001
    Location
    AVR Lovers Club
    Posts
    423

    Random Numbers

    i type this:

    Private Sub Form_Load()
    MsgBox rnd
    End Sub

    each time i start the program it displays the same exact number. It isn't supposed to do that. And it didn't use to do it either.
    My monkey wearing the fedora points and laughs at you.

  2. #2
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728
    Try this:
    VB Code:
    1. Private Sub Form_Load()
    2. Randomize Timer
    3. MsgBox rnd
    4. End Sub

    Randomize is a function which initialises VB's random number generator based on an argument (Timer in this case) in some way...
    Digital-X-Treme
    Contact me on MSN Messenger: [email protected]

    [VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
    / (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]

  3. #3
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    it is the same in debug mode since they want you to be able to test the results on any errors with the same number.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  4. #4
    DaoK
    Guest
    rnd random a number between 0 and 1 so if you want to have a random number between 7 and 10 you will need to do :

    rnd * 3 + 7

    That a little tip

  5. #5
    Originally posted by DaoK
    rnd random a number between 0 and 1 so if you want to have a random number between 7 and 10 you will need to do :

    rnd * 3 + 7

    That a little tip
    Here's a full function:
    VB Code:
    1. Public Function RandInt(lowerbound As Integer, upperbound As Integer)
    2.     RandInt = Int((upperbound - lowerbound + 1) * Rnd + lowerbound
    3. End Function

  6. #6
    DaoK
    Guest
    You should add Randomize in the Function

  7. #7
    Originally posted by DaoK
    You should add Randomize in the Function
    Like I said before, you should only call randomize when you start your program, otherwise you get a severe performace hit.

  8. #8
    DaoK
    Guest
    And it's should be Randomize Time or something like that someone told me to have more change to have a real Random number.

  9. #9
    Timer is the default argument for Randomize, so these two do the same thing. Trust me, I'm right, there was a thread a while back if you search for it.
    VB Code:
    1. Randomize
    2. Randomize Timer

  10. #10
    Addicted Member
    Join Date
    Jun 2001
    Location
    UK
    Posts
    158
    how do u do this if you want the no between -0.1 and 0.1
    [vbcode] On Error GoTo VBForums[/vbcode]
    www27.brinkster.com/muditha

  11. #11
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    hi Muds
    Rnd returns a number betw 0 and 1 and so u just need to multiply by the required range. U can add or subtract minor decimal roundings as required.
    Regards
    Stuart
    VB Code:
    1. Private Sub Command1_Click()
    2.     Randomize
    3.     Label1 = Format$(Rnd * 0.2 - 0.1, "0.000")
    4. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

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