Contact US

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.

Students Click Here




How do you get a random number between numbers like 40 and 50.  

A = int(15 * rnd + 1)

This gives 1 - 15.

A = int(50 * rnd + 40)

This gives numbers up to 80...what am I doing wrong...?

RE: Randomizing

INT((upperbound - lowerbound + 1) * RND + lowerbound)
A = INT((50 - 40 + 1) * RND + 40)
A = INT(11 * RND + 40)

RE: Randomizing

Think of it as a series of conversions of ranges. The RND function provides you with the range [0,1), and you want the range [40,51) (note the exclusion at the upper end). The first step should be to set the magnitude of the ranges. [40,51) is the same magnitude (size) as [0,11), so you need to multiply the value returned by RND by 11 (note that this value is the lower bound subtracted from the upper bound). After this point, you have the range [0,11), and you want to translate this up to [40,51). This is easy; just add 40.

The rule of converting ranges is that whatever you do your number you also do to both ends of the range.

That said, the following function will concisely return an integer inside the specified range, inclusively at both ends:

FUNCTION getRandomInteger%(leastNumber%, greatestNumber%)
rangeSize% = greatestNumber% - leastNumber% + 1
getRandomInteger% = (rangeSize% * RND) + leastNumber%

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! Already a Member? Login

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