Results 1 to 4 of 4

Thread: Random number generator

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 1999
    Location
    Turner ME USA
    Posts
    18

    Post

    I am making a program that utilizes a random number generator. The one I am using is from the VB Help files:

    T = Int((5 * Rnd) + 1)

    I seem to recall that this particular format is not all that random and there is a better way to do it. Does anyone know what that is or have any suggestions?

    Thanks!

    Jeff

  2. #2
    Guest

    Post

    Funny, I noticed that as well!!

    I't seems to need you to have "Randomize" (without the quotes) before you make a call to the RND function. E.G.

    Randomize
    T = Int((5 * Rnd) + 1)

    Must admit, though, the random number is still pretty predictable the fist time.

  3. #3
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    Post

    Yes that is true you must put Randomize in the formload but i don't get why do u have to put +1 at the end i have made a random numbers program just by making a function that has
    Code:
    Public Function RandomNumbers()
    RandomNumbers = int(Rnd * 10)
    end Function
    and then i just put this code in the cmdSpin
    lbl1.caption = RandomNumbers
    lbl2.caption = RandomNumbers
    lbl3.caption = RandomNumbers
    and it produces Different Random Numbers every time without using the + 1 . . . what does the + 1 do ?


    ------------------
    Cory Sanchez
    Young Student
    ICQ#: 18640149



  4. #4
    Member
    Join Date
    Jan 1999
    Location
    London, UK
    Posts
    58

    Post

    I've worked all this out before but unfortunately my VB PC has completely died so I can't get to any of the code Think this is right...

    Consider this function to generate a random number between known values:

    Public Function BoundedRND(ByVal iMin as Integer, ByVal iMax as Integer) as Integer

    BoundedRND = Int((iMax - iMin + 1) * Rnd + iMin)

    End Function

    This gets a random number in a range (iMax - iMin + 1), in your example 0 to 4, then adds the lower number (in your example 1) to put the returned range within the desired range.

    In the situation you're talking about, you are after an integer between 1 and 5: applying in BoundedRND gives:

    BoundedRND = Int((5 - 1 + 1) * Rnd + 1)

    = Int(5 * Rnd + 1)

    = 1..5

    So - the "+ 1" is just putting the random number so far generated within the bounds 1..n.

    Make sense?

    AndyC
    London

    ------------------
    * * * * * * * * * * * * * * * * * * * * * *
    * *
    * AndyC *
    * London *
    * email: [email protected] *
    * *
    * * * * * * * * * * * * * * * * * * * * * *


    [This message has been edited by Andy Collyer (edited 11-26-1999).]

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