Determining How Long Your Code Took To Run A Solution To Excel Running Slow How To Hide Excel Itself Opening Without A Blank Workbook And No Splash Screen Closing Excel Via Visual Basic with Application.Quit Playing WAV Files Running Macros That Are Located In A Different Workbook Writing Text To A Shape Or Text Box. Excel Macro Examples & Free Downloads One of the best ways to learn Excel VBA is by exploring code samples & examples. That is why, we publish detailed examples, downloadable workbooks & full-length tutorials from time to time. In this page, you can find several Excel VBA examples. Click on any link to see the.
- Microsoft Excel Visual Basic Code Samples Download
- Simple Visual Basic Examples
- Microsoft Excel Visual Basic Code Samples Printable
This Excel tutorial explains how to use the Excel RND function with syntax and examples.
Description
The Microsoft Excel RND function returns a random number that is greater than or equal to 0 and less than 1. You can use the RND function in a formula to generate a random number within a range of values.
The RND function is a built-in function in Excel that is categorized as a Math/Trig Function. It can be used as a VBA function (VBA) in Excel. As a VBA function, you can use this function in macro code that is entered through the Microsoft Visual Basic Editor.
Syntax
The syntax for the RND function in Microsoft Excel is:
Parameters or Arguments
- number
- Optional. It specifies what random number to return (see return values below).
Returns
The RND function returns a random number that is greater than or equal to 0 and less than 1.
If the number provided is greater than 0 or the number parameter is omitted, the RND function will return the next random number in the sequence using the previously generated random number as the seed.
If the number is less than 0, the RND function will return a random number using number as the seed.
If the number is 0, the RND function will return the random number that was most recently generated.
If the number provided is greater than 0 or the number parameter is omitted, the RND function will return the next random number in the sequence using the previously generated random number as the seed.
If the number is less than 0, the RND function will return a random number using number as the seed.
If the number is 0, the RND function will return the random number that was most recently generated.
Random Integer Range
To create a random integer number between two values (range), you can use the following formula:
Where lowerbound is the smallest number and upperbound is the largest number that you want to generate a random number for.
The formula above would generate an integer random number between 10 and 25.
Applies To
- Excel for Office 365, Excel 2019, Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000
Microsoft Excel Visual Basic Code Samples Download
Example (as VBA Function)
The RND function can only be used in VBA code in Microsoft Excel.
Let's look at some Excel RND function examples and explore how to use the RND function in Excel VBA code:
In this example, the variable called LRandomNumber would now contain a random number between 200 and 300.
Add the Randomize function
If you find that you are not getting a truly random number when calling the RND function, you can use the RANDOMIZE function to change the seed value for the RND function's random number generator. The call to the RANDOMIZE function should preceed the call to the RND function.
Simple Visual Basic Examples
For example,
In this example, the variable called LRandomNumber would now contain a random number between 200 and 300. The Randomize function would ensure that the number generated is truly random by initializing the random number generator with a seed value that is equivalent to the system timer.
Microsoft Excel Visual Basic Code Samples Printable
Warning: If you don't call the Randomize function before calling the Rnd function, the Rnd function may return the same random number value each time. And therefore, you may not get a truly random number.