Friday, August 12, 2016

How to use the RANDOMIZE Function (VBA)

Description

The Microsoft Excel RANDOMIZE function allows you to change the seed value used by the random number generator for the RND function.

Syntax

The syntax for the RANDOMIZE function in Microsoft Excel is:
Randomize ( [ seed ] )

Parameters or Arguments

seed
Optional. It is a numeric seed that will be used by the RND function to generate a random number. If no seed value is provided, Excel will use the system timer as the seed value for the RND function.

Applies To

  • Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000

Type of Function

  • VBA function (VBA)

Example (as VBA Function)

The Randomize function can only be used in VBA code in Microsoft Excel.
Let's look at some Excel Randomize function examples and explore how to use the Randomize function in Excel VBA code:
For example:
'Example provided by techonthenet.com

Sub Macro1

Dim LRandomNumber As Integer

Randomize
LRandomNumber = Int ((300 - 200 + 1) * Rnd + 200)

End Sub
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.
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.

No comments:

Post a Comment