An Improved Custom Random Numbers Function


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
      RandomNumbers = Int((Highest + 1 - Lowest) * Rnd + Lowest)
      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, Precisionis 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...


Read 63 times
Dylan Austin


Whenever I have a problem, I sing. Then I realize my voice is worse than my problem.

We use cookies to improve our website. By continuing to use this website, you are giving consent to cookies being used. More details…