Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Random Record Selection Problems.
Message
 
 
To
15/12/2003 15:22:26
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00859259
Message ID:
00859261
Views:
16
Store the value before using it:
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 RandomList
A 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() ) # 0
In fact this last technique negates the need for a RandSel column. i.e.
Select * ;
	From finallist ;
	Into Table RandomList
	Where AScan( aSelection, RecNo() ) # 0
censored.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform