Results 1 to 10 of 10

Thread: Producing a random number from a range

  1. #1

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    36

    Exclamation Producing a random number from a range

    I'm trying to make a random number from -100 to 100 in VB6, but all that's coming out is negative numbers. This is what I have:


    Code:
    Randomize
    x = int(rnd * 100) - 100
    Any ideas?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Producing a random number from a range

    The Rnd function returns a number from 0 to just under 1, so int(rnd * 100) will give you a number from 0 to 99, and subtracting 100 from that will give you -100 to -1

    You should be changing *100 to something else.

  3. #3

    Thread Starter
    Member
    Join Date
    May 2008
    Posts
    36

    Re: Producing a random number from a range

    Ah, so would this be it?
    Code:
    int(rnd * 200) - 100

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Producing a random number from a range

    That will return (0 to 199) - 100 , which is -100 to 99

  5. #5
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Producing a random number from a range

    Quote Originally Posted by MountainDew7
    Ah, so would this be it?
    Code:
    int(rnd * 200) - 100
    Not quite, but this does it:
    Code:
    Int(Rnd * 201) - 100
    That will include random numbers ranging from -100 to +100 inclusive.
    Doctor Ed

  6. #6
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Producing a random number from a range

    The formula is: x = Int(Rnd * (Hi - Lo + 1)) + Lo
    Here you have: Lo = -100, Hi = 100
    So: x = Int(Rnd * (100 - (-100) + 1)) + (-100)
    or: x = Int(Rnd * 201) - 100
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  7. #7
    New Member
    Join Date
    Oct 2008
    Posts
    11

    Re: Producing a random number from a range

    what if u just wanted a small number like 1 to 6?

  8. #8
    New Member
    Join Date
    Oct 2008
    Posts
    11

    Re: Producing a random number from a range

    it would just be Int(Rnd * 7)

  9. #9
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Producing a random number from a range

    Quote Originally Posted by hendersondayton
    it would just be Int(Rnd * 7)
    No. Please read AnHn's post showing the formula. For a range from 1 to 6 inclusive, Lo = 1, Hi = 6. Got it?
    Doctor Ed

  10. #10
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Producing a random number from a range

    Quote Originally Posted by hendersondayton
    what if u just wanted a small number like 1 to 6?

    it would just be Int(Rnd * 7)
    Int(Rnd * 7) give you a random integer between 0 to 6 inclusive.

    To get a random number between 1 and 6, use the formula in my post#6 above:
    x = Int(Rnd * (6 - 1) + 1) +1
    or
    x = Int(Rnd * 6) + 1
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

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