Category : Dbase (Clipper, FoxBase, etc) Languages Source Code
Archive   : NN0505.ZIP
Filename : RANDOM.PRG
Filename: Randlib.prg
Author: A. Timothy Wong
Date: September 14, 1990
Version: Clipper 5.0
Notes: Compile with: /n/w/a
Generalize Random functions for generating
random numbers, random unique filenames,
random strings etc...
Copyright (c) 1990, Nantucket Corporation
ALL RIGHTS RESERVED.
*/
// Declaration Code
#include "FileIO.CH"
#define START_PLATE 3
#define LEFT_FILE 6
#define RIGHT_FILE 3
#define FILEHEAD "$R"
STATIC Seed // Seed used by RANDOM() function
// Used by RandomString, also user changeable.
STATIC BitMap := ;
"AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" + ;
"1234567890-#,."
// Executable Code
FUNCTION Randomize
// Initializes our Seed with value based on PI, Date & Time.
Seed := (0.3141285793 + (DAY(DATE()) + ;
MONTH(DATE()) + YEAR(DATE()))/1000.0 + ;
SECONDS()/1000.0)
RETURN NIL
FUNCTION Random
// Simple random number generator
// User can substitue with their own algorithm
Seed := Seed * 997
Seed := Seed - INT(Seed)
RETURN(Seed)
FUNCTION RandomTemplate(nLSide,nRSide)
// Function to return Random Number with
// formatted as nLSide.nRSide
STATIC lGenNeg := .F.
LOCAL nOutNum
LOCAL cTempStr
LOCAL nJunk
cTempStr := TRANSFORM(Random(),"9.99999999999999999999")
IF nLSide = 1 .AND. nRSide = 0
nOutNum := INT(VAL(SUBSTR(cTempStr,7,1)) * 1.0)
ELSE
IF .NOT. lGenNeg
nOutNum := ;
VAL(SUBSTR(cTempStr,START_PLATE,nLSide))
nOutNum += VAL("." + ;
SUBSTR(cTempStr,START_PLATE + ;
nLSide,nRSide))
ELSE
nOutNum := ;
VAL(SUBSTR(cTempStr,START_PLATE,nLSide - 1))
nOutNum += VAL("." + ;
SUBSTR(cTempStr,START_PLATE + ;
nLSide,nRSide))
nOutNum *= -1
ENDIF
ENDIF
lGenNeg := IIF( INT(Random()*10) % 2 > 0, !lGenNeg, .F.)
RETURN (nOutNum)
FUNCTION RandomString(nMinLen,nMaxLen,lStrRepeat)
// Function that generates a random string
// nMinLen - minimum len for return string
// nMaxLen - maximum len for return string
// lStrRepeat - is this string to be repeated
LOCAL nDif, cRetStr, nTemp
nDif := INT(nMaxLen - nMinLen)
nTemp := nMinLen + ;
(ABS(RandomTemplate(LEN(LTRIM(STR(nDif))),0)) % ;
(nDif + 1))
IF lStrRepeat
cRetStr := ;
REPLICATE(RandomBitMap(BitMap,1),nTemp)
ELSE
cRetStr := RandomBitMap(BitMap,nTemp)
ENDIF
RETURN (cRetStr)
FUNCTION NewBitMap(cNewBitMap)
RETURN (BitMap := cNewBitMap)
FUNCTION RandomBitMap(cBitMap,nLen)
// Function that generates a random string from a set of
// characters cBitMap = STRING of characters in bit map
// nLen = Length of String to return
LOCAL nLenBitMap,nNumLen,i,cRetStr := SPACE(0)
nLenBitMap := LEN(cBitMap)
nNumLen := LEN(LTRIM(STR(nLenBitMap)))
FOR i := 1 TO nLen
cRetStr += ;
SUBSTR(cBitMap, (ABS(RandomTemplate(nNumLen,0)) % ;
nLenBitMap) + 1, 1)
NEXT
RETURN (cRetStr)
FUNCTION RandomDate(nDays)
// Returns a date +/- nDays
LOCAL nJunk, nTemp
nTemp := Random()
nJunk := INT((nTemp*Seconds() % nDays))
RETURN IIF( nTemp < 0.5 ,DATE() + nJunk,DATE() - nJunk)
FUNCTION RandomLogical
STATIC lLogic := .T.
RETURN lLogic := !lLogic
FUNCTION RandomFile
// Generates a random filename & returns filename
// Will make sure that it is unique
LOCAL cFileName := SPACE(12)
LOCAL cFileStr := "0123456789"
LOCAL lDone := .T.
cFileName := FILEHEAD + ;
RandomBitMap(cFileStr,LEFT_FILE) + ;
"." + RandomBitMap(cFileStr,RIGHT_FILE)
IF FILE(cFileName)
WHILE lDone
cFileName := FILEHEAD + ;
RandomBitMap(cFileStr,LEFT_FILE) + ;
"." + RandomBitMap(cFileStr,RIGHT_FILE)
lDone := FILE(cFileName)
END
ENDIF
RETURN cFileName
FUNCTION RandomArray(nLen,nMin,nMax)
// Returns an array of randomly generated (integer)
// numbers between nMin & nMax
// nLen - Length of array to generate
// nMin - Minimun number to generate
// nMax - Maximum number to generate
LOCAL aReturn
LOCAL nDiff,I,nLenDiff
aReturn := ARRAY(nLen)
nDiff := INT(nMax - nMin)
nLenDiff := LEN(LTRIM(STR(nDiff)))
FOR I := 1 TO nLen
aReturn[I] := ;
INT((RandomTemplate(nLenDiff,0) % (nDiff + 1)) + ;
nMin)
NEXT
RETURN (aReturn)
FUNCTION RandomRange(nLow,nHigh)
// Returns a random number between nLow & nHigh
LOCAL nDiff := nHigh - nLow
LOCAL nTemp
nTemp := Random() * (10^LEN(STR(nHigh))) % nDiff
RETURN (nLow + nTemp)
// EOF: Random.prg
Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!
This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.
But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/