Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generate a Random Number? 1

Status
Not open for further replies.

torres12

Programmer
Jul 25, 2007
3
CA
Hi all.

How can I generate a random number between something like 1 and 5, with just Clipper 5.3 (no add-ons products)?

Thanks,
torres
 
I use this for non serious random number generation:

The N parameter controls the size of the random number.

I can't remember if it works out as 0 based or 1 based, you'll have to check for yourself!
Code:
FUNCTION RND_POS(N)
	STATIC XRNDSEED := .123456789
	IF XRNDSEED = .123456789
		XRNDSEED += VAL(SUBSTR(TIME(), 7, 2)) / 100
	ENDIF
	XRNDSEED := (XRNDSEED * 31415821 + 1) / 1000000
	RETURN INT( (XRNDSEED -= INT(XRNDSEED)) * N)

Regards

Griff
Keep [Smile]ing
 
Another one which I found on a BBS back in the 90's.
Sorry, I don't know who wrote it, but it works:

Code:
* Function...RAND() function
* Syntax.....RAND(expN)
* Parameters.A numeric expression that evaluates to an integer.
* Returns....An integer in the range of 0 to n-1.
* Notes......Requires that a memory variable called "seed" be
*            initialized to a value between 0 and 1 before calling
*            For example:
*               seed = VAL(SUBSTR(TIME(),8,1)) * .1

FUNCTION RAND
PARAM n

seed = 997 * seed
seed = seed - INT(seed)

RETURN (INT(n * seed))

Jock

PS: xHarbour has the hb_random built-in function.
 
Thanks you Jock.
Torres is not that impressed

Regards

Griff
Keep [Smile]ing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top