Generate random numbers

To generate random numbers between 0 and 1 you can use the Excel RAND function and apply into Excel or VBA

Example: Generate random numbers

Generate random numbers

METHOD 1. Generate random numbers

EXCEL

=RAND()
This formula uses the Excel RAND function to generate a random number between 0 and 1. The RAND function generates a random number every time a worksheet is calculated.

METHOD 1. Generate random numbers using VBA

VBA

Sub Generate_random_numbers()
'declare a variable
Dim ws As Worksheet
Set ws = Worksheets("Analysis")
'generate a random number between 0 and 1
ws.Range("B5") = Rnd

End Sub

PREREQUISITES
Worksheet Name: Have a worksheet named Analysis.
ADJUSTABLE PARAMETERS
Output Range: Select the output range by changing the cell references ("B5") in the VBA code to any cell in the worksheet, that doesn't conflict with the formula.

METHOD 2. Generate random numbers using VBA with a For Loop

VBA

Sub Generate_random_numbers()
'declare a variable
Dim ws As Worksheet
Set ws = Worksheets("Analysis")

'generate multiple random numbers by looping through B5 and B6

For x = 5 To 6

ws.Range("B"&x) = Rnd
Next

End Sub

PREREQUISITES
Worksheet Name: Have a worksheet named Analysis.
ADJUSTABLE PARAMETERS
Output Column Range: Select the output column range by changing the column references ("B") in the VBA code to any cell in the worksheet, that doesn't conflict with the formula.
Output Row Range: Select the output row range by changing the for and to x values in the VBA code, that don't conflict with the formula.

Explanation about the formula used to generate random numbers

EXPLANATION

EXPLANATION
This tutorial shows and explains how to generate random numbers between 0 and 1, by using an Excel formula or VBA.

Excel Method: This tutorial provides a single Excel method that can be applied to generate random numbers between 0 and 1, using the Excel RAND function.

VBA Methods: This tutorial provides a two VBA methods. The first method generates one random number. The second method generates multiple random numbers through the use of the For Loop method.

FORMULA
=RAND()
ADDITIONAL NOTES
Note 1: The RAND function generates a new random number every time a worksheet is calculated. This includes opening a workbook.

RELATED TOPICS

Related Topic Description Related Topic and Description
How to return the most frequently occurring text using Excel and VBA methods

RELATED FUNCTIONS

Related Functions Description Related Functions and Description
The Excel RAND function returns a random number between 0 and 1