SELECT finallist ALTER table finallist ADD COLUMN RandSel n(1,0) FOR i = 1 TO INT(reccount()*.2) iRandomRow = INT(RAND()*RECCOUNT()) GOTO iRandomRow IF finallist.RandSel <> 1 then replace RandSel WITH 1 ELSE SKIP 1 replace RandSel WITH 1 ENDIF ENDFOR SELECT * from finallist WHERE RandSel = 1 INTO TABLE RandomListA better solution might be:
Rand(-1) iRowsSelected = 0 Update FinalList ; Set RandSel = 1 ; Where RecNo() = Int( Rand() * RecCount()) ; And RandSel <> 1 ; And fRowsToSelect( RecCount() * 0.2, @iRowsSelected ) Return .T. ****************************************************** Function fRowsToSelect( iRowsToSelect, iRowsSelected ) ****************************************************** iRowsSelected = iRowsSelected + 1 Return iRowsSelected < iRowsToSelect EndFunc && fRowsToSelect( iRowsToSelect, iRowsSelected )Or if performance is an issue, you might want to generate RecCount() * 0.2 random RecNo()'s before doing the update - so that the SQL Update processes the file sequentially. Example:
Rand(-1) iRowsToSelect = RecCount() * 0.2 Dimension Array aSelection[ iRowsToSelect ] For iRow = 1 To iRowsToSelect iRandomRow = Int( Rand() * RecCount()) Do While AScan( aSelection, iRandomRow ) # 0 iRandomRow = Int( Rand() * RecCount()) EndDo aSelection[ iRow ] = iRandomRow Next iRow ASort( aSelection ) Update FinalList ; Set RandSel = 1 ; Where AScan( aSelection, RecNo() ) # 0In fact this last technique negates the need for a RandSel column. i.e.
Select * ; From finallist ; Into Table RandomList Where AScan( aSelection, RecNo() ) # 0