rickus,

Here is a demo that I personally belive gives you exactly what you are wanting, plus some.

With the following demo, you define at the top the minimum number wanted, the maximum number wanted, how many numbers you want found (by sizing the RndNumbers array to how ever many you want), and the size of the margin you want. Please note, the margin definition is for one side (where you two sides to make your buffer - see the code for details).

Then the rest of the code takes care of everything.

If you set the min to max distance to close to each other, the demo we adjust the number of numbers it will provide you at the end. For example, if you set the min value to 1 and the max value to 5, then run it, the demo will only return one number.

This demo does use a little brute force, but I believe it is cut down to a bare minimum.

With no further ado, here is the code:

`CONST MinNumber = 1`

CONST MaxNumber = 100

CONST Margin = 5 ' DEFINED AS ONE-SIDED

DIM RndNumbers(5) AS LONG

DIM Index AS INTEGER

DIM InSideANumMargin AS _BYTE

DIM NotInRangeCount AS LONG

Index = LBOUND(RndNumbers)

SCREEN _NEWIMAGE(600, 800, 32)

T% = TIMER

RANDOMIZE T%

PRINT "T%: "; T%

' GET INTEGER NUMBERS

DO

RndNumbers(Index) = RND * (ABS(MaxNumber) - ABS(MinNumber)) + 1

InSideANumMargin = 0

' IT'S NOT THE FIRST SO LET'S LOOP FROM THE FIRST ARRAY INDEX TO THE ONE BEFORE THE CURRENT INDEX

' TO MAKE SURE THE CURRENT ARRAY VALUE ISN'T WITHIN THE MARGINS OF THE OTHERS

FOR i = LBOUND(RndNumbers) TO Index - 1

IF RndNumbers(Index) >= (RndNumbers(i) - Margin) AND RndNumbers(Index) <= (RndNumbers(i) + Margin) THEN

InSideANumMargin = 1

END IF

NEXT

IF InSideANumMargin = 0 THEN

Index = Index + 1

END IF

' MAKE SURE THIS ISN'T THE FIRST ARRAY INDEX OR THE LAST. IF SO, SKIP IT.

IF (Index - 1) >= LBOUND(RndNumbers) AND Index <= UBOUND(RndNumbers) THEN

' DEFINE THE CurrentIndex TO THE MINIMUM NUMBER

CurrentIndex = MinNumber

DO

' RESET SOME VALUES

MatchesAvailable = 0

NotInRangeCount = 0

' SEARCH THROUGH ALL ARRAY VALUES TO SEE IF CurrentIndex IS WITHIN THEIR MARGINS

FOR I2 = LBOUND(rndnumbers) TO Index - 1

' CHECK TO SEE IF CurrentIndex IS WITHIN THE MARGINS OF THE CURRENT ARRAY VALUE

IF CurrentIndex < (RndNumbers(I2) - Margin) OR CurrentIndex > (RndNumbers(I2) + Margin) THEN

' INCREMENT NOT-IN-RANGE-COUNT IF CurrentIndex IS OUTSIDE OF CURRENT ARRAY VALUE'S MARGINS

NotInRangeCount = NotInRangeCount + 1

END IF

NEXT

' CHECK TO SEE IF CurrentIndex IS NOT IN RANGE OF ANY OF THE VALUES IN THE ARRAY.

' FOR THIS TO BE TRUE, THE NotInRangeCount MUST EQUAL THE NUMBER ARRAY VALUES DEFINED.

' IF CurrentIndex IS FOUND TO BE INSIDE ANY OF THE DEFINED ARRAY VALUE MARGINS, THEN

' THE CURRENT INDEX IS NOT USABLE.

IF NotInRangeCount = (Index - LBOUND(rndnumbers)) THEN

' WE HAVE A MATACH TO AN OPEN INDEX SLOT

MatchesAvailable = 1

' LEAVE THIS LOOP

EXIT DO

END IF

' IF WE MADE IT HERE, THEN WE NEED INCREMENT THE CurrentIndex

CurrentIndex = CurrentIndex + 1

' IF THE CurrentIndex IS LESS THAN THE MAXNUMBER, CONTINUE THE LOOP UNTIL THE END

LOOP WHILE CurrentIndex <= MaxNumber

' WE NEED TO CHECK TO SEE IF WE HAVE SCANNED THE ENTIRE SPECTRUM

IF MatchesAvailable = 0 THEN

' AT THIS POINT, THERE ARE NO MORE POSSIBLE MATCHES, SO THERE IS NO NEED TO CONTINUE ON.

Index = Index - 1

EXIT DO

END IF

END IF

LOOP WHILE Index <= UBOUND(rndnumbers)

PRINT

IF Index > UBOUND(rndnumbers) THEN Index = UBOUND(rndnumbers)

' DISPLAY ALL THE NUMBERS WE WERE ABLE TO FIND WITHIN GIVEN RANGE WITH GIVEN MARGINS

IF Index = UBOUND(rndnumbers) THEN

PRINT "ALL "; (UBOUND(rndnumbers) + 1); " ARRAY INDEX'S HAVE BEEN DEFINED!"

ELSE

PRINT "ONLY "; (Index + 1); " OUT OF "; (UBOUND(rndnumbers) + 1); " ARRAY INDEX'S WERE DEFINED!"

END IF

PRINT

FOR i = LBOUND(rndnumbers) TO Index

PRINT "RndNumbers("; i; "): "; RndNumbers(i)

NEXT

_DISPLAY

Let me know close this is to what you are wanting.

Happy Coding!

Walter Whitman

The Joyful Programmer

www.TheJoyfulProgrammer.comwww.TheJoyfulProgrammer.com/qb64/forum/