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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Numbers 1

Status
Not open for further replies.

sheronne

Programmer
Jun 1, 2001
71
US
I want to return random numbers between <low Number> and <high Number> where I designate the low and high. I know how to return the value us the Rand() function but I don't want duplicate values returned. I know how to code it to check variables against one another, I have even tried using a table to store the value and check the table each time it returns a Random number. I was just wanting other opinions and ideas thinking someone might know an easier.
 
1. You have started a thread some time back regarding generating random numbers. So I am not taking about those and also about seed (+/-) parameters etc..
2. Let us assume
myLow = someLowValue say 1000
myHigh = someHighValue say 9999

DO WHLE .t.
myRand = RAND() && with whatever seed etc...
myfactor = CEILING(mylow / myRand)
myNewNumber = myRAND * myFactor
IF myNewNumber > myHigh
LOOP
ENDIF
** If myNumber is not unique then also LOOP
EXIT
ENDDO

Hope this idea helps.


ramani :-9
(Subramanian.G)
FoxAcc
ramani_g@yahoo.com
 
I don't think there's an easier way to avoid duplicates. I personally would use an array rather than a table if the spread between low and high wasn't greater than 65000.

Jim
 
*****************************************************
** 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)
*****************************************************
FUNCTION GenRand
PARAMETERS nLow, nHigh, nHowMany

PUBLIC nRandArray(nHowMany)

** Exit the function if improperly called
IF PARAMETERS() < 3
WAIT &quot;PARAMETERS ERROR&quot;
RETURN .f.
ENDIF
IF nLow > nHigh OR nHowMany < 1
WAIT &quot;PARAMETERS ERROR&quot;
RETURN .f.
ENDIF
**

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.
*****************************************************
** EOF
*****************************************************
ramani :-9
(Subramanian.G)
FoxAcc
ramani_g@yahoo.com
 
The parameters input check contained a snag. The corrected one is posted. Sorry for the error........
It happens when I cut & paste from the production area..
*****************************************************
** 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

PUBLIC nRandArray(nHowMany)

** Exit the function if improperly called
IF PARAMETERS() < 3
WAIT &quot;PARAMETERS ERROR&quot;
RETURN .f.
ENDIF
IF nLow > nHigh OR nHowMany < 1 OR nHowMany > (nHigh-nLow)
WAIT &quot;PARAMETERS ERROR&quot;
RETURN .f.
ENDIF
**

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.
*****************************************************
** EOF
*****************************************************
You can cut & paste the above and save it as GenRand.Prg
ramani :-9
(Subramanian.G)
FoxAcc
ramani_g@yahoo.com
 
Yet again I pasted an earlier copy...
I lost control on the versions and I did not test what I posted. Note the Public Array is shifted down to catch the errors. I am realy sorry. Hope this time I am posting my latest copy. (Actualy I am digging up my old project and posting this).
*****************************************************
** 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 &quot;PARAMETERS ERROR&quot;
RETURN .f.
ENDIF
IF nLow > nHigh OR nHowMany < 1 OR nHowMany > (nHigh-nLow)
WAIT &quot;PARAMETERS ERROR&quot;
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.
*****************************************************
** EOF
*****************************************************
ramani :-9
(Subramanian.G)
FoxAcc
ramani_g@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top