INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • 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.

Jobs

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 Smileing

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.

RE: Generate a Random Number?

(OP)
Thanks guys.

torres
 

RE: Generate a Random Number?

Thanks you Jock.
Torres is not that impressed

Regards

Griff
Keep Smileing

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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

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:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close