A random number generator generates random numbers and that's all it does.
Tom beat me to it, but ...
If you are generating random numbers between 1 and 10 and want 5 of them then chances are you will get duplicates.
If you want to exclude duplicated numbers then you will need to store the numbers you have already generated and ignore any repeats of those numbers. You can do this by storing the numbers in a temp-table, a character string or however you want to do it.
So, if you had a table "table1" with 2 fields "field1" and "field2" you could do:
You could use this include file to insert random table searches in any program.
The first paramter in the include file is the table to be randomly sampled.
The second parameter in the include file is a procedure to be run for each of your sampled records.
Tom beat me to it, but ...
If you are generating random numbers between 1 and 10 and want 5 of them then chances are you will get duplicates.
If you want to exclude duplicated numbers then you will need to store the numbers you have already generated and ignore any repeats of those numbers. You can do this by storing the numbers in a temp-table, a character string or however you want to do it.
Code:
/* randv.i */
DEFINE TEMP-TABLE ttrandom NO-UNDO
FIELD seq AS INTEGER
FIELD rrowid AS ROWID
INDEX idx1 seq.
DEFINE TEMP-TABLE ttfound NO-UNDO
FIELD rrowid AS ROWID
INDEX idx1 rrowid.
DEFINE VARIABLE iseq AS INTEGER NO-UNDO.
DEFINE VARIABLE irandom AS INTEGER NO-UNDO.
DEFINE VARIABLE rfound AS ROWID NO-UNDO.
DEFINE VARIABLE iMaxRecords AS INTEGER NO-UNDO.
DEFINE VARIABLE iNumberSearches AS INTEGER NO-UNDO.
FUNCTION RandomRowid RETURNS ROWID (pii_seq AS INTEGER):
FIND ttrandom WHERE ttrandom.seq = RANDOM (1,pii_seq) NO-LOCK NO-ERROR.
IF AVAILABLE ttrandom THEN RETURN ttrandom.rrowid.
END FUNCTION.
UPDATE iNumberSearches LABEL "Number of Records to Find"
iMaxRecords LABEL "Max Records in Search"
WITH SIDE-LABELS 1 DOWN THREE-D FRAME f1.
HIDE FRAME f1.
RUN ipRandomise.
RUN ipStoreTable.
RUN ipFindRandomRecords.
FOR EACH ttfound NO-LOCK,
EACH {1} WHERE ROWID({1}) = ttfound.rrowid NO-LOCK:
RUN {2}.
END.
PROCEDURE ipStoreTable:
EMPTY TEMP-TABLE ttrandom.
ASSIGN iseq = 0.
FOR EACH {1} NO-LOCK:
CREATE ttrandom.
ASSIGN iseq = iseq + 1
ttrandom.seq = iseq
ttrandom.rrowid = ROWID ({1}).
IF iMaxRecords > 0 AND iseq >= iMaxRecords THEN LEAVE.
END.
END PROCEDURE.
PROCEDURE ipFindRandomRecords:
EMPTY TEMP-TABLE ttfound.
ASSIGN irandom = 0.
REPEAT:
ASSIGN rfound = RandomRowid (iseq).
FIND ttfound WHERE ttfound.rrowid = rfound NO-LOCK NO-ERROR.
IF NOT AVAILABLE ttfound THEN DO:
CREATE ttfound.
ASSIGN ttfound.rrowid = rfound
irandom = irandom + 1.
END.
IF irandom >= iNumberSearches THEN LEAVE.
END.
END PROCEDURE.
PROCEDURE ipRandomise:
DEFINE VARIABLE irandloop AS INTEGER NO-UNDO.
DO irandloop = 1 TO (TIME * INT (STRING (TODAY,"999999")) MOD 100000):
RANDOM (0,irandloop).
END.
END PROCEDURE.
So, if you had a table "table1" with 2 fields "field1" and "field2" you could do:
Code:
{randv.i "table1" "ipDisplay"}
FORM WITH FRAME f2.
PROCEDURE ipDisplay:
DISPLAY table1.field1 table2.field2 WITH FRAME f2 DOWN.
DOWN WITH FRAME f2.
END PROCEDURE.
You could use this include file to insert random table searches in any program.
The first paramter in the include file is the table to be randomly sampled.
The second parameter in the include file is a procedure to be run for each of your sampled records.