|
-
May 8th, 2012, 10:56 AM
#1
Thread Starter
Addicted Member
[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.
-
May 8th, 2012, 11:10 AM
#2
Hyperactive Member
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.
-
May 8th, 2012, 11:15 AM
#3
Thread Starter
Addicted Member
Re: Random from 2 numbers (not between 2 numbers)
 Originally Posted by samuelk
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.
-
May 8th, 2012, 11:15 AM
#4
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:
Dim sec As Integer
Dim int As Integer
sec = DateTime.Now.Second
If sec Mod 2 Then
int = 3
Else
int = 7
End If
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.
-
May 8th, 2012, 11:18 AM
#5
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
 
-
May 8th, 2012, 11:21 AM
#6
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.
-
May 8th, 2012, 11:26 AM
#7
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
 
-
May 8th, 2012, 11:29 AM
#8
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|