GENERATING RANDOM INTEGERS
By Edward F. Moneo

More and more of us are requiring random integers in our QB programs.
Some of us can write the statement for generating a random integer by heart.
However, I'm going to lay down the rules for those of you that don't know them
by heart.

RANDOMIZE TIMER
This statement seeds the random number generator.
This statement must appear at the beginning of your program, at least before
the first RND statement.

Without the RANDOMIZE TIMER, the RND statement(s) of the program will always
generate the exact same set of random numbers. This could be handy for
debugging the program.


The following DIM statements are for proper definition of the variables:

DIM LOWER   AS INTEGER   'The FROM or LOWER value of the required random range.
DIM UPPER   AS INTEGER   'The TO or UPPER value of the required random range.
DIM RANDINT AS INTEGER   'The generated random integer within the range.

The following is the statement for generating a random integer:

RANDINT = INT ((UPPER - LOWER + 1)*RND + LOWER)


Depending on your QB manual, you may also see the following statement:

RANDINT = INT(RND * (UPPER - LOWER + 1)) + LOWER


Either statement works equally well.


CONSIDERATIONS:

1) Abbreviated statements.
   Some programmers will often abbreviate the RND statement as follows.

   Example #1: Get random integers from 1 to 10.
   RANDINT = INT(RND * (10 - 1 + 1)) + 1
   They often abbreviate this to:
   RANDINT = INT(RND * 10) + 1

   Example #2: Get random integers from 0 to 9.
   RANDINT = INT(RND * (9 - 0 + 1)) + 0
   They often abbreviate this to:
   RANDINT = INT(RND * 10)

   These shortcuts can lead to problems, especially when you need to modify
   the upper or lower values of the range. The best approach is to place the
   complete RND statement, with the LOWER and UPPER parameters, into a
   subroutine, SUB or FUNCTION, and always pass both the lower and upper
   values of the range.

2) Unique random integers.
   Random integers can and will repeat like the throws of the dice.
   Some applications need the random integers to always be unique.
   Here's a sample bit of code to eliminate duplicate random integers.

   
RANDOMIZE TIMER
DIM LOWER   AS INTEGER
DIM UPPER   AS INTEGER
DIM RANDINT AS INTEGER

LOWER =  1
UPPER = 52

DIM DUP (LOWER TO UPPER)   'Setup an array covering all the possibilities.

FOR X = LOWER TO UPPER
    DO
      RANDINT = INT(RND * (UPPER - LOWER + 1)) + LOWER
    WHILE DUP(RANDINT) = 1
    DUP(RANDINT) = 1
    PRINT RANDINT
NEXT X

The above FOR loop will generate 52 unique random integers.
Within the above DO loop, we get a random integer. We check the DUP array
to see if that value is flagged with a 1, indicating that we had it before.
If we had it before, it's a duplicate, so ignore it and get another random
integer. If it's unique, continue and flag it in the DUP array to indicate
that we just had it, and now process it (in this example, print it).


Hope this all works for you, or at least gives you some ideas.

Regards,
Moneo
