Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create UNIQUE RANDOM Numbers ?

Usefull Functions & Procedures

How to create UNIQUE RANDOM Numbers ?

by  ramani  Posted    (Edited  )
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 :)
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top