ExcelTip.com
ExcelTip.com
Account Icon Account Icon Account Icon
Google Exceltip.com
JOIN OUR NEWSLETTER
  and receive for joining:
Free eBook Learn More!
Free Excel ADD-IN
Free Weekly Excel Tip
4 e-books in cd-rom
F1 Formulas & Functions
F1 Formulas & Functions
F1 EXCEL
F1 eBook (Spanish)
F1 EXCEL
Financial Statements.xls
 

» An Improved Custom Random Numbers Function
Tip Contributed by Martin Green.Martin Green, Office Tips is a freelance Access and Excel trainer and developer based in London.

In an earlier tutorial (More Custom Functions for Access and Excel) I included a custom function for generating random numbers within a chosen range. The function allowed the user to specify a lower limit and a higher limit, and generated a random number that fell somewhere within the range.

The function generated a whole number. But what about people who don't want a whole number? It occurred to me that with just a little more code, I could write a function that allowed the user to specify how many decimal places they required. For currency, for example, they might specify 2. It wasn't as complex as I had expected...

Here's the code:

Public Function RandomNumbers(Lowest As Long, Highest As Long, _
Optional Decimals As Integer)
   Application.Volatile  'Remove this line to "freeze" the numbers
   If IsMissing(Decimals) Or Decimals = 0 Then
      Randomize
      RandomNumbers = Int((Highest + 1 - Lowest) * Rnd + Lowest)
   Else
      Randomize
      RandomNumbers = Round((Highest - Lowest) * Rnd + Lowest, Decimals)
   End If
End Function

How does it work?

The function accepts 3 arguments: Lowest being the lower limit of the required range, Highest being the higher limit, and an optional one Decimals where the user can specify a number of decimal places from 0 to 9 (nine being the maximum that the random number generator can provide). Because there is a limit to the number of digits the random number generator can provide, the more digits you want on the left of the decimal point, the fewer you are given on the right. From 0-9 you can have up to 9 decimal places, from 10-999 you can have 8, from 1000-99999 you can have 7, and so on. Also, although I have declared the data type for the Highest argument as Long (the long integer data type allows numbers in the range minus 2,147,483,648 to plus 2,147,483,647 ...aren't you glad you know that?) you won't get more than 7 digits to the left of the decimal place either.

  • The first line of code specifies Application.Volatile which makes this a "volatile" function. Each time the worksheet calculates the random numbers you have created will recalculate themselves so you'll get a different set. You can "freeze" the numbers by performing a Copy and Paste Special > Values on the range. But if you don't want that, just omit the line.
  • Next comes an If Statement which uses the IsMissing function to check whether or not a number of decimal places has been specified, or if the user has specified zero decimal places. If either of these are the case a calculation is performed using the Rnd function to generate a random number. The Int function turns it into a whole number (integer).
  • The Else part of the If Statement happens if an entry greater than zero is specified for the number of decimal places. A calculation is performed to generate a random number, as before, but this time it is nom made into a whole number. Instead the Round function (it works like this: Round(Number, Precision) is used to limit the number of decimal places.

How do you use it?

Here are some examples of the function in use in an Excel worksheet.

Example 1: Random numbers between 50 and 100 with no decimal places (whole numbers)...

Example 2: Random numbers between 50 and 100 with 2 decimal places (trailing zeros not shown so some appear to have less. The 79.9 in cell A4 is actually 79.90)...

Example 3: Random numbers between zero and 1 with 4 decimal places...

Example 4: Random numbers between 10,000 and 15,000 with 2 decimal places...



Rate this tip
12 34 5
  RATING: 3.76
  VIEWS: 35308

READER COMMENTS (view all comments)


No comments have been submitted.


REGISTERED USERS - Click here to post comments


GUESTS (Click here to register)
Name
Comment Title
Comments

Accounting for Dummies

Absolute Beginner's Guide to Microsoft Excel 2002

422 Tax Deductions for Businesses and Self-Employed Individuals : You Get a Raise Every Time You Find a Legitimate Tax Deduction

How to Use Financial Statements: A Guide to Understanding the Numbers

Financial Reporting and Analysis (2nd Edition)

Microsoft Word Version 2002 Step By Step (With CD-ROM)

RELATED MICROSOFT EXCEL TIPS


Convert PDF Files to Excel






Excel VBA books
Accounting books
Business Plans
MS Office books
Taxes books

VIEW ALL BOOKS


  Advertise With Us                               

Tips

Add-In in VBA | Applications - Word, Outlook in VBA | Array Formulas | Cells, Ranges, Rows, and Columns in VBA | Counting | Custom Functions | Custom Functions in VBA | Database Formulas | Database in VBA | Date & Time Formulas | Date & Time in VBA | Events in VBA | Excel 2003 | Excel Chart | Excel Consolidating | Excel Counting | Excel Custom Functions using VBA | Excel Customizing | Excel Data | Excel Dates | Excel Editing | Excel Files | Excel Filter | Excel Format | Excel Formula | Excel General | Excel Grouping and Outlining | Excel Importing Text Files | Excel Information | Excel Keyboard Shortcuts | Excel Loan Formulas | Excel Macros - VBA | Excel Pivot Tables | Excel Printing | Excel Range Name | Excel Security - Protection | Excel Sorting | Excel Style | Excel Subtotals | Excel Summing | Excel Text | Excel Time | Excel Tools | Excel Worksheet, Workbook | Files, Workbook, and Worksheets in VBA | Financial Formulas | Formating in VBA | General Topics in VBA | Import and Export in VBA | Information Formulas | Keyboard & Other Shortcuts in VBA | Keyboard Formula Shortcuts | Links between Worksheet and Workbooks | Links in VBA | Logical Formulas | Lookup Formulas | Mail - Send and Receive in VBA | Menus, Toolbars, Status bar in VBA | Modules, Class Modules in VBA | Other Q&A Formulas | Printing in VBA | Protecting in VBA | Summing | Text Formulas | User Forms, Input boxes in VBA | Using Loops | Working with Formulas |

Tips by Version

Microsoft Excel 97 | Microsoft Excel 2000 | Microsoft Excel 2002 | All Microsoft Excel Versions | New in Excel 2002 | New in Excel 2003 - Office 11

Website

Home | Tip of Hour | Recommended Tips | Most Viewed Tips | Tips by Version | Submit a Tip | My Tips
Microsoft Excel Tutorials | Excel Links | Write for Us | About Us | Search Results | Tip Archives | Excel Forum | Excel Forum Archives

Excel Book

Excel 97 Book | Excel 2000 Book | Excel 2002 Book | Excel XP Book | Book Store

Terms and Conditions of use
The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.

Copyright © 2003 ExcelTip.com
Microsoft, Microsoft Excel is a U.S. registered trademark of Microsoft Corporation
Site Developed By: Varien