Random selection of records
Author Message
Random selection of records

Greetings.. was wondering if anyone had any quick tips on printing "n"
records from a data file.. for example, a .dbf with 50 records, what would
be the easiest way to randomly select 10 for printing?  Any ideas would be

*************************************************************************
*  Ralph Young                        * "If you're not part of the      *
*  U.S. Patent & Trademark Office     *  solution then you're part of   *
*  703-308-4211                       *  the problem."                  *

*************************************************************************
*  Any opinions are mine alone and do not reflect the views of          *
*  the U.S. Patent & Trademark Office                                   *
*************************************************************************

Sun, 17 Nov 1996 21:44:32 GMT
Random selection of records
: Greetings.. was wondering if anyone had any quick tips on printing "n"
: records from a data file.. for example, a .dbf with 50 records, what would
: be the easiest way to randomly select 10 for printing?  Any ideas would be
: greatly appreciated.  Thanks in advance.

You could use the RAND function, as follows:

=RAND(-1) && Seed the random-number generator
FOR m.Count = 1 TO 10
GO (INT(RAND() * 49 + 1))  && will generate a number 1-50
* Print the record
ENDFOR

I don't know how "random" the RAND function is (I suspect there may be
better random functions than FoxPro's), but this should work pretty
well.

If you want to make sure that a record doesn't get repeated (though it
wouldn't be random then), try keeping track of the numbers as follows:

=RAND(-1)
DIMENSION Numbers[10] && A place to store used numbers
Numbers = -1  && Set them all to a number we'll never get
FOR m.Count = 1 TO 10
m.Rec = INT(RAND() * 49 + 1)
DO WHILE ASCAN(Numbers, m.Rec) != 0  && Loop until we get a new number
m.Rec = INT(RAND() * 49 + 1)
ENDDO
Numbers[m.Count] = m.Rec
GO (m.Rec)
ENDFOR

Hope this will help.
Pete Kruckenberg

Mon, 18 Nov 1996 11:24:27 GMT
Random selection of records
-> Greetings.. was wondering if anyone had any quick tips on printing "n"
-> records from a data file.. for example, a .dbf with 50 records, what would
-> be the easiest way to randomly select 10 for printing?  Any ideas would be
-> greatly appreciated.  Thanks in advance.

How do you want to print them?  Are you using a programmed report or one of the
report writing tools?

Anyways, provided you had a small set, create an array with as many elements as
Then using the RAND() function, proceed to generate integers in the range of
1-N, where N is the number of records.
Because you cannot reuse the values, go to that element in the array.
If the value is 0 then either print the record or copy to a holding
table. Also increment your found record counter.
Increase the value in the array.
Stop when you have found your desired number of records.

Regards,

Derek Jubb
Manager, Information Systems

Thu, 21 Nov 1996 22:20:00 GMT
Random selection of records
Quote:

>-> Greetings.. was wondering if anyone had any quick tips on printing "n"
>-> records from a data file.. for example, a .dbf with 50 records, what would
>-> be the easiest way to randomly select 10 for printing?  Any ideas would be
>-> greatly appreciated.  Thanks in advance.

Try this...

mytable = sys(3)
select *, .f. as include into table (mytable)
[ do some random function to set your ten records ]
report form <blahblah> for include
erase (mytable-".dbf")

Sat, 30 Nov 1996 11:54:00 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages