Why Rnd() function generates same number twice?
Posted: Wed Jun 22, 2005 12:55 pm
Hi,
Here is my code for generating Unique Random number keeping in mind that Rnd() function will not generate random number twice...I have distinctIds file which contain already used Numbers...if the number is present in the file then this routine doesnt generate that number again...
but if distincID file is empty the routine generates random non uniqe numbers...Please help me if there is some modification required in the code below.
$IFNDEF JOBCONTROL.H
$INCLUDE DSINCLUDE JOBCONTROL.H
$ENDIF
$DEFINE TESTING
* Declare "bitmap" strings to be in named COMMON area of memory, so that their
* values persist across multiple rows processed.
COMMON /FP1/UniqueIndex
If Len(UniqueIndex) = 1
Then
GoSub BuildBitmaps
End
GoSub GenerateRandom
GoSub CheckExistingRndNo
* Initialize return variable.
Ans = 0
* Generate Unique Number between 1 and 9
**********************************************************************************************
GenerateRandom:
**********************************************************************************************
BaseNumber = 1
UniqueIndexTmp = Rnd(4)
UniqueIndexNo=BaseNumber +UniqueIndexTmp
Ans = UniqueIndexNo
GoSub CheckExistingRndNo
Return(Ans)
**********************************************************************************************
CheckExistingRndNo:
**********************************************************************************************
UniqueIndexFound = UniqueIndex[UniqueIndexNo,1]
If UniqueIndexFound
Then
Message = "Results:"
Message<-1> = "UniqueIndexFound = " :UniqueIndexNo
Message<-1> = "Number Exists Generate New One"
Call DSLogInfo(Message, " ** TESTING ** ")
GoSub GenerateRandom
End
Else
Message = "Results:"
Message<-1> = "UniqueIndexFound = " :UniqueIndexNo
Message<-1> = "Number Does not Exists Pass the number to the Job"
End
Ans= UniqueIndexNo
Return(Ans)
**********************************************************************************************
BuildBitmaps:
**********************************************************************************************
*
* Here we construct the "bitmaps" in COMMON variables. This step is executed only if required
* (that is, usually, only on the first row processed).
UniqueIndex= Str("0",
FileError = 1
OpenSeq "D:\files\DistinctIds.txt" To InputFvar
On Error
Message = 'Error (code ' : Status() : ') opening "input.txt" file.'
End
Locked
Message = 'File "DistinctIds.csv" locked by another process.'
End
Then
FileError = 0
Message = 'File "DistinctIds.csv" opened succcessfully.'
Loop
While ReadSeq Line From InputFvar
UniqueIndex[Line,1] = "1"
Repeat
Closeseq InputFvar
End
Else
Message = 'Unable to open "DistinctIds.csv" file for reading.'
End ; * end of OpenSeq statement
Here is my code for generating Unique Random number keeping in mind that Rnd() function will not generate random number twice...I have distinctIds file which contain already used Numbers...if the number is present in the file then this routine doesnt generate that number again...
but if distincID file is empty the routine generates random non uniqe numbers...Please help me if there is some modification required in the code below.
$IFNDEF JOBCONTROL.H
$INCLUDE DSINCLUDE JOBCONTROL.H
$ENDIF
$DEFINE TESTING
* Declare "bitmap" strings to be in named COMMON area of memory, so that their
* values persist across multiple rows processed.
COMMON /FP1/UniqueIndex
If Len(UniqueIndex) = 1
Then
GoSub BuildBitmaps
End
GoSub GenerateRandom
GoSub CheckExistingRndNo
* Initialize return variable.
Ans = 0
* Generate Unique Number between 1 and 9
**********************************************************************************************
GenerateRandom:
**********************************************************************************************
BaseNumber = 1
UniqueIndexTmp = Rnd(4)
UniqueIndexNo=BaseNumber +UniqueIndexTmp
Ans = UniqueIndexNo
GoSub CheckExistingRndNo
Return(Ans)
**********************************************************************************************
CheckExistingRndNo:
**********************************************************************************************
UniqueIndexFound = UniqueIndex[UniqueIndexNo,1]
If UniqueIndexFound
Then
Message = "Results:"
Message<-1> = "UniqueIndexFound = " :UniqueIndexNo
Message<-1> = "Number Exists Generate New One"
Call DSLogInfo(Message, " ** TESTING ** ")
GoSub GenerateRandom
End
Else
Message = "Results:"
Message<-1> = "UniqueIndexFound = " :UniqueIndexNo
Message<-1> = "Number Does not Exists Pass the number to the Job"
End
Ans= UniqueIndexNo
Return(Ans)
**********************************************************************************************
BuildBitmaps:
**********************************************************************************************
*
* Here we construct the "bitmaps" in COMMON variables. This step is executed only if required
* (that is, usually, only on the first row processed).
UniqueIndex= Str("0",
FileError = 1
OpenSeq "D:\files\DistinctIds.txt" To InputFvar
On Error
Message = 'Error (code ' : Status() : ') opening "input.txt" file.'
End
Locked
Message = 'File "DistinctIds.csv" locked by another process.'
End
Then
FileError = 0
Message = 'File "DistinctIds.csv" opened succcessfully.'
Loop
While ReadSeq Line From InputFvar
UniqueIndex[Line,1] = "1"
Repeat
Closeseq InputFvar
End
Else
Message = 'Unable to open "DistinctIds.csv" file for reading.'
End ; * end of OpenSeq statement