×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Generate a Random Number?

## Generate a Random Number?

(OP)
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

### RE: Generate a Random Number?

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 ing

### RE: Generate a Random Number?

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.

(OP)
Thanks guys.

torres

### RE: Generate a Random Number?

Thanks you Jock.
Torres is not that impressed

Regards

Griff
Keep ing

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!