Results 1 to 8 of 8

Thread: [RESOLVED] Random from 2 numbers (not between 2 numbers)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    Resolved [RESOLVED] Random from 2 numbers (not between 2 numbers)

    Hello guys. I need a simple way to get a random number from 2 numbers. This means that for example if my 2 numbers are 3 and 7, i don't need a random number between 3 and 7 that would be like 3,4,5,6,7 but only a random that is either 3 or 7. Meaning not all the numbers between x and y, but either x or y.

  2. #2
    Hyperactive Member
    Join Date
    Aug 2011
    Posts
    272

    Re: Random from 2 numbers (not between 2 numbers)

    If you're only ever going to need two options, I would think the simplest way would be to assign each of the two values to an integer (say IntA and IntB), generate a random number between 1 and 2, then have an if statement that does whatever you need to IntA if the random value is 1 or IntB if the random value is 2.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    Re: Random from 2 numbers (not between 2 numbers)

    Quote Originally Posted by samuelk View Post
    If you're only ever going to need two options, I would think the simplest way would be to assign each of the two values to an integer (say IntA and IntB), generate a random number between 1 and 2, then have an if statement that does whatever you need to IntA if the random value is 1 or IntB if the random value is 2.
    lol y that was my exact same thought.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: Random from 2 numbers (not between 2 numbers)

    Another way is to get datetime.now.second, if the second is odd then it's 3 if it's even then it's 7. Like this:
    vb Code:
    1. Dim sec As Integer
    2.         Dim int As Integer
    3.  
    4.         sec = DateTime.Now.Second
    5.  
    6.         If sec Mod 2 Then
    7.             int = 3
    8.         Else
    9.             int = 7
    10.         End If
    11.  
    12.         MsgBox(int)
    edit - that would only work for 2 numbers. Simply because there can only be an odd and only an even number.
    Last edited by dday9; May 8th, 2012 at 11:18 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Random from 2 numbers (not between 2 numbers)

    That approach has some real advantages, but it has a very serious disadvantage if the code is called too often: It won't be random.

    It would be somewhat better if you did the same thing with the millisecond rather than seconds, but either way you don't want to be doing that if you are calling it often. It should be highly random if the code calling it is infrequent (several seconds apart), and not predictably periodic.
    My usual boring signature: Nothing

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: Random from 2 numbers (not between 2 numbers)

    @Shaggy
    That's true, so long as the user wouldn't call it very often it would be a good solution. If it does need to be called often tehn samuelk's response is better.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Random from 2 numbers (not between 2 numbers)

    Yes, but I suspect that your approach will be faster...as long as it is called in an appropriate fashion.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    Re: Random from 2 numbers (not between 2 numbers)

    it doesn't really matter much to me if it's "3" or "7", so if it comes out 9 times out of 10 as "3", I don't mind it, it's just to make things look a lil bit better to my professor, but it doesn't affect at all what I'm trying to achieve, so the time method works perfectly fine as well.

    All I need this random for is because I'm finding the equation of a line between 2 points, and to do that, to find b in the y=mx+b formula, I can use either (x1, y1) or (x2, y2) and in my case 3 and 7 are actually point 1 and point 2, so either of them is fine, but thanks guys, all of you.

    EDIT:
    Things can be mixed, too. I can simply random between x & y, if the random is even I go for 3, if it's y I go for 7.

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