How to Create an Array of Unique Random Numbers (136420)



The information in this article applies to:

  • Microsoft Visual FoxPro for Windows 3.0
  • Microsoft FoxPro for MS-DOS 2.0
  • Microsoft FoxPro for MS-DOS 2.5
  • Microsoft FoxPro for MS-DOS 2.5a
  • Microsoft FoxPro for MS-DOS 2.5b
  • Microsoft FoxPro for MS-DOS 2.6
  • Microsoft FoxPro for MS-DOS 2.6a
  • Microsoft FoxPro for Windows 2.5
  • Microsoft FoxPro for Windows 2.5a
  • Microsoft FoxPro for Windows 2.5b
  • Microsoft FoxPro for Windows 2.6
  • Microsoft FoxPro for Windows 2.6a
  • Microsoft FoxPro for Macintosh 2.5b
  • Microsoft FoxPro for Macintosh 2.5c
  • Microsoft FoxPro for Macintosh 2.6a
  • Microsoft FoxPro for UNIX 2.6

This article was previously published under Q136420

SUMMARY

The FoxPro RAND() function returns a random number between 0 and 1. This article shows by example how to:

  • Create random numbers between 5 and 10.
  • Easily create an array of random numbers between any two integers.
  • Make the random numbers in an array unique.
The user-defined functions (UDFs) listed in this article demonstrate how.

MORE INFORMATION

Step-by-Step Example

  1. Create a program file called Myudf.prg that contains the following code:
       FUNCTION RANDOM
       *  Returns a random number between any 2 numbers.
       *  Syntax: =RANDOM(Lower Value, Higher Value)
       *  Example:  lnX=RANDOM(5,10)
       PARAMETER anLow,anHigh
       lnRnumber=INT((RAND()*(anHigh-anLow+1))+anLow)
       RETURN lnRnumber
    
       FUNCTION A_RANDOM
       *  Returns an array of random numbers between any 2 numbers.
       *  The array must already be defined
       *  Syntax:   =A_RANDOM(ArrayName,Lower Value, Upper Value)
       *  Example:  =A_RANDOM('gaMyarray',17,25)
       PARAMETER lcA_name,anLow,anHigh
       lnAlength=ALEN(&lcA_name)
       FOR lni=1 TO lnAlength
         &lcA_name(lni)=INT((RAND()*(anHigh-anLow+1))+anLow)
       ENDFOR
       RETURN &lcA_name
    
       FUNCTION U_RANDOM
       *  Returns an array of unique random numbers  between any 2 numbers.
       *  The array must already be defined
       *  Syntax:   =U_RANDOM(Array Name, Lower Value, Upper Value)
       *  Example:  =U_RANDOM('gaMyarray',1,10)
       PARAMETER lcA_name,anLow,anHigh
       lnAlength=ALEN(&lcA_name)
       IF lnAlength>anHigh-anLow
          lnAlength=anHigh-anLow+1
       ENDIF
       &lcA_name=.F.
       lni=1
       DO WHILE lni<=lnAlength
          lnRnumber=INT((RAND()*(anHigh-anLow+1))+anLow)
          llNextnum=.T.
          IF lnRnumber=anLow .OR. lnRnumber=anHigh
             DO CASE
             CASE lnRnumber=anLow
                lnK=1
                DO WHILE lnK>0
                   anLow=anLow+1
                   lnK=ASCAN(&lcA_name,anLow)
                ENDDO
             CASE lnRnumber=anHigh
                lnK=1
                DO WHILE lnK>0
                   anHigh=anHigh-1
                   lnK=ASCAN(&lcA_name,anHigh)
                ENDDO
             ENDCASE
          ELSE
             lnK=ASCAN(&lcA_name,lnRnumber)
             IF lnK>0
                llNextnum=.F.
             ENDIF
          ENDIF
          IF llNextnum
             &lcA_name(lni)=lnRnumber
             lni=lni+1
          ENDIF
       ENDDO
    						
  2. Save and Close Myudf.prg
  3. Test the functions:

    1. To print a single random number between 5 and 10, type the following in the Command window:
            SET PROCEDURE TO MYUDF.PRG
            ? RANDOM(5,10)
      								
    2. To populate an array with random numbers, type the following in the Command window:
            SET PROCEDURE TO MYUDF.PRG
            DIMENSION gaMyarray(10)
            =A_RANDOM('gaMyarray',1,10)
      								
    3. If you want to populate an array with unique random numbers, type the following in the Command window:
            SET PROCEDURE TO MYUDF.PRG
            DIMENSION gaMyarray(10)
            U_Random('gaMyarray',1,10)

Modification Type:MajorLast Reviewed:12/3/2003
Keywords:KB136420 kbAudDeveloper