Thursday, October 21, 2010

How do I have a random number between 48 and 90 but exclude the numbers 58 to 64 VB 2008?

I can create a random number that goes from 48 to 90 but I can't seem to find a way to exclude the numbers 58-64 from this group. Does anyone know how to do this? In case you didn't see; I am using VB 2008.How do I have a random number between 48 and 90 but exclude the numbers 58 to 64 VB 2008?
I can think of 2 ways to do it:

Generate random number between 48 and 90 in a do while loop. If the number is 58-64, then repeat the loop. Fall out if not.

This is simple to implement. Will waste a few CPU cycles generating numbers that get thrown away, but it probably doesn't matter.



There are 36 possible numbers in your set (I'm assuming 48 and 90 are valid...if not you'll have to adjust things here a bit).

So you could generate a random number R in the range 0 to 35

If R is %26lt; 10, then R = R + 48

else R = R + 55



R will be a random number that fits your needs, and no looping required.

No comments:

Post a Comment