Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

Sys(3), Sys(2015)

These two functions generate random strings. SYS(3) returns an 8-character string suitable for a filename. SYS(2015) returns a 10-character string handy for a procedure or function name—it was added in FoxPro 2.0 to allow GENSCRN, the 2.x screen generator, to generate procedure names for snippet code.

Usage

cString = SYS(3)
cString = SYS(2015)

SYS(3) is driven by the system clock and has a big problem. If you call it in succession too quickly, you can get the same string back. On a Pentium/90 with 24MB memory, calling SYS(3) a thousand times in a tight loop produced only 134 different values, each of them seven or eight times. On a Pentium Pro/200 with 64MB of memory, the same code produced only 82 distinct values, most of them 12 or 13 times. On the other hand, using SYS(2015) in the same loop produced 1,000 different values on both machines.

Also, keep in mind that two workstations calling one of these functions at the same time will get the same value. They’re not suited to generating unique IDs.

You do occasionally need a name for a temporary file. Your best bet is to use SYS(3) or SYS(2015) in a loop until you get one that doesn’t exist. See the example.

Beware: The strings returned by SYS(3) always are composed entirely of digits. This means they can’t serve as aliases for tables. So, while you can create a table named with the value returned, when you open it, you’ll want to specify a legal alias. If you don’t, VFP assigns the table an alias based on the work area in which it’s opened.

Example

* Get a unique filename for a text file
* On a busy network, you may want to try to FCREATE()
* the file inside the loop to make sure someone else hasn't
* just grabbed the same name.
cFile = SYS(3)
DO WHILE NOT FILE(cFile+".TXT")
   cFile = SYS(3)
ENDDO

See Also

File(), Rand()