Random Sample
Author Message

 Posted: Visual FoxPro General, Random Sample Top
 I have a table containing 3773 records, and i need a random sample of 2500 of these records. Does is there any easy way to to this w/ foxpro or do i have to come up w/ an algorithm myself Thanks in advance,Mike Visual FoxPro2

Tamar E. Granor

 Posted: Visual FoxPro General, Random Sample Top
 The Rand() function will let you do this. Something along these lines: * Assume nNumToPick is the number you * want to select * Seed RAND() RAND(-1) * Set up a cursor to know which records you * already have. PK here represents the primary * key for your table. SELECT PK, .F. AS lChosen ; FROM YourTable ; INTO CURSOR Picked nRecCount = _TALLY nTotalPicked = 0 DO WHILE nTotalPicked < nNumToPick nRec = INT(_TALLY * RAND()) + 1 GO nRec IN Picked IF NOT lChosen REPLACE lChosen WITH .T. nTotalPicked = nTotalPicked + 1 ENDIF ENDDO SELECT * ; FROM YourTable ; JOIN Picked ; ON YourTable.PK = Picked.PK ; WHERE lChosen ; INTO CURSOR RandomSet Tamar

CetinBasoz

 Posted: Visual FoxPro General, Random Sample Top
 rand(-1)select top 2500 rand() as randomVal, * from myTable order by 1 into cursor random2500

SharathMum

 Posted: Visual FoxPro General, Random Sample Top
 nRec = Reccount("YourTable")  && assuming no deleted records rand(-1)select top 2500  Int(nRec * Rand())+1 as randomVal, * from myTable order by 1 into cursor random2500i have combined the solution post by Tamar E. Granor & CetinBasoz

CetinBasoz

 Posted: Visual FoxPro General, Random Sample Top
 Why there is a need to combine and use reccount()

SharathMum

 Posted: Visual FoxPro General, Random Sample Top

 rand(-1)select top 2500 rand() as randomVal, * from myTable order by 1 into cursor random2500

If I execute your (cetinBasoz) statement I get randomVal in fractions,

But if I change rand () as randomVal to Int(nRec * Rand())+1 as randomVal (by Tamar E.Granor) it gives me the required result

He used _Tally which gets update if table process command is executed, so I used recount()

Credited goes to u two for your simplicity and for his accuracy

CetinBasoz

 Posted: Visual FoxPro General, Random Sample Top
 It's not a matter of crediting. I think neither Tamar nor me needs crediting (at least she has an enormously high credit among VFP community). I simply can't see the benefit of multiplying with reccount(). How does it give 'required' result How does it affect accuracy If you think that column values would match with the actual recno() of their source rows then you're mistaken.

SharathMum

 Posted: Visual FoxPro General, Random Sample Top
 its mental blindness i was under the influence of Tamar example, so i was seeing your example also from the record number point of view,whereas in your eg. its the top 2500 of randomVal