Microsoft KB Archive/136420

= How to Create an Array of Unique Random Numbers =

Article ID: 136420

Article Last Modified on 12/3/2003

-

APPLIES TO


 * 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

-



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.



Step-by-Step Example
  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  Save and Close Myudf.prg Test the functions:

  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)   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)   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) </ol> </li></ol>

Additional query words: VFoxWin FoxWin FoxDos FoxUnix

Keywords: KB136420

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.