You are currently offline, waiting for your internet to reconnect

How to Create an Array of Unique Random Numbers

This article was previously published under Q136420
SUMMARY
The FoxPro RAND() function returns a random number between 0 and 1. Thisarticle 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)
VFoxWin FoxWin FoxDos FoxUnix
Properties

Article ID: 136420 - Last Review: 12/03/2003 14:42:09 - Revision: 2.1

  • Microsoft Visual FoxPro 3.0 Standard Edition
  • Microsoft FoxPro 2.0
  • Microsoft FoxPro 2.5b for MS-DOS
  • Microsoft FoxPro 2.5a
  • Microsoft FoxPro 2.5b for MS-DOS
  • Microsoft FoxPro 2.6 for MS-DOS
  • Microsoft FoxPro 2.6a Standard Edition
  • Microsoft FoxPro 2.5b
  • Microsoft FoxPro 2.5a
  • Microsoft FoxPro 2.5b
  • Microsoft FoxPro 2.6 Standard Edition
  • Microsoft FoxPro 2.6a Standard Edition
  • Microsoft FoxPro 2.5b for Macintosh
  • Microsoft Visual FoxPro 2.5c for Macintosh
  • Microsoft FoxPro 2.6a Professional Edition for Macintosh
  • Microsoft FoxPro 2.6 for SCO/UNIX
  • KB136420
Feedback