You can cut & paste the following code and save the file as GenRand.Prg
*****************************************************
** FUNCTION NAME ... GenRand
** Author .......... Subramanian.G, FoxAcc
** .......... ramani_g@yahoo.com
** Purpose ......... Generates required number of
** Unique random numbers (nHowMany)
** between the nLow & nHigh passed
** HowRun........... DO GenRand WITH nHigh,nLow,nHowMany
** ... or..... =GenRand(nHigh,nLow,nHowMany)
** Limitations...... Size limited by Array
*****************************************************
** FUNCTION GenRand
PARAMETERS nLow, nHigh, nHowMany
** Exit the function if improperly called
IF PARAMETERS() < 3
WAIT "PARAMETERS ERROR"
RETURN .f.
ENDIF
IF nLow > nHigh OR nHowMany < 1 OR nHowMany > (nHigh-nLow)
WAIT "PARAMETERS ERROR"
RETURN .f.
ENDIF
**
PUBLIC nRandArray(nHowMany)
LOCAL nMyRand, nCount, nDuplicate
nCount = 1
nDuplicate = 0
DO WHILE nCount < nHowMany
nMyRAND = INT(((nHigh-nLow+1)* RAND() ) + nLow)
nDuplicate = ASCAN(nRandArray,nMyRand)
IF nDuplicate # 0
LOOP
ENDIF
nRandArray(nCount) = nMyRand
nCount=nCount+1
ENDDO
RETURN .t.
**********************************************************
** Ramani (Subramanian.G), FoxAcc, ramani_g@yahoo.com
** EOF
**********************************************************
Evaluate this to make others know how useful is this
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.