Randomize, with Exceptions

1. Senior Member
Join Date
Dec 1969
Posts
343

## Randomize, with Exceptions

Hello,<BR><BR>I would like to use the Randomize and Rnd and I am a little to using them so I am curious if I can :<BR><BR>Pull random numbers out of a Range? Say 1 to 100<BR><BR>BUT THAT IS NOT IT.......<BR><BR>I really want to :<BR><BR>Pull random numbers out of a Range that is missing some numbers.<BR><BR>HA!......<BR><BR>Say the Range is 1 to 100, BUT there is no 57 or 39 etc...<BR><BR>Can the Function be modified to handle that exception?<BR><BR>Thank you for your help, I really love these boards they help tremendously as do most of the posters....

2. Senior Member
Join Date
Dec 1969
Posts
16,931

## Easiest way is...

...just to have a function that returns a random integer between X and Y. Then, if the number it returns is in your exceptions, call the function again.<BR><BR>[code language="VBScript"]<BR>Function GenerateRandomNumber(iMin, iMax)<BR> GenerateRandomNumber = iMin+Int(Rnd() * (iMax-iMin))<BR>End Function<BR><BR>Randomize<BR>Dim iRandom<BR>Do<BR> iRandom = GenerateRandomNumber(1, 100)<BR>Loop While iRandom = 57<BR>[/code]<BR><BR>I may be +/-1 out with the whole range thing. Always takes me ages to work that out!<BR><BR>Craig.

3. Senior Member
Join Date
Dec 1969
Posts
343

## RE: Good idea, but...

... according to your Do Loop wont it only loop until it finds 57?<BR><BR>Maybe I am missing something........

4. Senior Member
Join Date
Dec 1969
Posts
16,931

## Better

[code language="VBScript"]<BR>Function GenerateRandomNumber(iMin, iMax)<BR> GenerateRandomNumber = iMin+Int(Rnd() * (iMax-iMin))<BR>End Function<BR><BR>Function IsException(intValue, arrIntExceptions)<BR> IsException = False<BR> If IsArray(arrIntExceptions) = False Then Exit Function<BR> IsException = True<BR> Dim intCounter<BR> intValue = Int(intValue)<BR> For intCounter = 0 To UBound(arrIntExceptions)<BR> If intValue = Int(arrIntExceptions(intCounter)) Then Exit Function<BR> Next<BR> IsException = False<BR>End Function<BR><BR>Function GetRandomNumber(iMin, iMax, arrExceptions)<BR> Dim iReturn<BR> Do<BR> iReturn = GenerateRandomNumber(iMin, iMax)<BR> Loop While IsException(iReturn, arrExceptions)<BR> GetRandomNumber = iReturn<BR>End Function<BR><BR>Randomize<BR>Dim iRandom<BR>Do<BR> iRandom = GenerateRandomNumber(1, 100)<BR>Loop While iRandom = 57<BR>[/code]<BR><BR>Or something like that.<BR><BR>Craig.

5. Senior Member
Join Date
Dec 1969
Posts
16,931

## RE: Good idea, but...

No. In that case, it will look WHILE it returns 57.<BR><BR>Have a look below. I actually wrote all the code and then forgot to call my new function, but you get the idea.<BR><BR>Craig.

6. Senior Member
Join Date
Dec 1969
Posts
343

## RE: Or something like that, how....

...in the world did you spit that out so quick????<BR><BR>Very nice give me an hour and I think I can understand it. <BR><BR>Thank you for your help.. I bet I can use this, I will work with it .<BR><BR>Thank you.......

7. Senior Member
Join Date
Dec 1969
Posts
16,931

## Here we go, third time's...

...the charm.<BR><BR>[code language="VBScript"]<BR>Function GenerateRandomNumber(iMin, iMax)<BR> GenerateRandomNumber = iMin+Int(Rnd() * (iMax-iMin))<BR>End Function<BR><BR>Function IsException(intValue, arrIntExceptions)<BR> IsException = False<BR> If IsArray(arrIntExceptions) = False Then Exit Function<BR> IsException = True<BR> Dim intCounter<BR> intValue = Int(intValue)<BR> For intCounter = 0 To UBound(arrIntExceptions)<BR> If intValue = Int(arrIntExceptions(intCounter)) Then Exit Function<BR> Next<BR> IsException = False<BR>End Function<BR><BR>Function GetRandomNumber(iMin, iMax, arrExceptions)<BR> Dim iReturn<BR> Do<BR> iReturn = GenerateRandomNumber(iMin, iMax)<BR> Loop While IsException(iReturn, arrExceptions)<BR> GetRandomNumber = iReturn<BR>End Function<BR><BR>Randomize<BR>Dim iRandom<BR>Do<BR> iRandom = GetRandomNumber(1, 100, Array(57, 36))<BR>Loop While iRandom = 57<BR>[/code]<BR><BR>Will get a random value between 1 and 100, excluding values 57 and 36.<BR><BR>Again, it may actually do 1 and 99... Probably 1 and 99. Ugh, work it out ;-)<BR><BR>Craig.

8. Senior Member
Join Date
Dec 1969
Posts
16,931

## RE: Or something like that, how....

The basic idea&#039;s simple... Fleshing it out with a few functions isn&#039;t too hard.<BR><BR>Just wasting time until I&#039;ve got to go pick the Mrs up ;-)<BR><BR>Craig.

9. Senior Member
Join Date
Dec 1969
Posts
343

## RE: Oh, I get it....

...it keep looping WHILE it is 57, OH OK....thx...

10. Senior Member
Join Date
Dec 1969
Posts
343

## RE: WOW, Thanks a ton.

.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•