In this article, we will create a custom function to generate random decimal numbers between the defined ranges.
Raw data for this example consists of lowest value and highest value of the range and number of decimal places required in decimal numbers.
We have created a custom function “RandomNumbers” to generate random decimal numbers between the defined lowest and highest numbers. This function takes lowest value, highest value and number of decimal places required as input and returns random decimal numbers as output.
Randomize function is used to initialize the RnD function of the random-number generator and assign it a new seed value.
RnD function returns a random number of single data type.
Round function is used to limit the number of decimal places.
Syntax of Round function
Please follow below for the code
Option Explicit Function RandomNumbers(Lowest As Double, Highest As Double, _ Optional Decimals As Integer = 0) Randomize RandomNumbers = Round((Highest - Lowest) * Rnd + Lowest, Decimals) End Function
We would love to hear from you, do let us know how we can improve our work and make it better for you. Write to us at firstname.lastname@example.org
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.