<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="VB6Parse Library Reference - rnd - Math">
<title>rnd - Math - VB6Parse Library Reference</title>
<link rel="stylesheet" href="../../../assets/css/style.css">
<link rel="stylesheet" href="../../../assets/css/docs-style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css">
<script src="../../../assets/js/theme-switcher.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/vbnet.min.js"></script>
<script>hljs.highlightAll();</script>
</head>
<body>
<header class="docs-header">
<div class="container">
<h1><a href="../../../index.html">VB6Parse</a> / <a href="../../../library/index.html">Library</a> / <a href="../../../library/functions/math/index.html">Math</a> / rnd</h1>
<p class="tagline">VB6 Library Reference</p>
</div>
</header>
<nav class="docs-nav">
<div class="container">
<a href="../../../index.html">Home</a>
<a href="../../../library/index.html">Library Reference</a>
<a href="../../../documentation.html">Documentation</a>
<a href="https://docs.rs/vb6parse" target="_blank">API Docs</a>
<a href="https://github.com/scriptandcompile/vb6parse" target="_blank">GitHub</a>
<button id="theme-toggle" class="theme-toggle" aria-label="Toggle theme">
<span class="theme-icon">🌙</span>
</button>
</div>
</nav>
<main class="container">
<article class="library-item">
<h1 id="rnd-function">Rnd Function</h1>
<p>Returns a Single containing a pseudo-random number.</p>
<h2 id="syntax">Syntax</h2>
<pre><code class="language-vbnet">Rnd[(number)]</code></pre>
<h2 id="parameters">Parameters</h2>
<ul>
<li><code>number</code> - Optional. Single or any valid numeric expression.</li>
</ul>
<h2 id="return-value">Return Value</h2>
<p>Returns a <code>Single</code> value greater than or equal to 0 and less than 1.
The behavior depends on the <code>number</code> argument:</p>
<table>
<thead>
<tr>
<th>Number Value</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
<td>< 0</td>
<td>Same number every time, using <code>number</code> as the seed</td>
</tr>
<tr>
<td>> 0</td>
<td>Next random number in sequence</td>
</tr>
<tr>
<td>= 0</td>
<td>Most recently generated number</td>
</tr>
<tr>
<td>Omitted</td>
<td>Next random number in sequence</td>
</tr>
</tbody>
</table>
<h2 id="remarks">Remarks</h2>
<p>The <code>Rnd</code> function returns a pseudo-random number from a deterministic sequence. It is used for generating random values in games, simulations, testing, and sampling.
Each new number is the product of the previous number times a constant (a) plus another constant (c).
The result is kept modulo a third number (m), which is invariably the width of its ‘long’ integer size (or fraction thereof).
Standard examples are m=2^64, 2^48 or 2^32.
Mathematically, this is represented as: r(i+1)=r(i)*a+c (mod m), where r(0) is the ‘seed’ value.
Although the algorithm in use can be obtained from analyzing a string of outputs from the generator and deducing the values of a, c and M.
Microsoft has published their constants on their web site and it turns out that they use an LCPRNG with:
```a = 16598013, c = 2820163 and m=2^24 = 16777216</p>
<pre><code>[Reference](http://www.noesis.net.au/main/Resources/Resources/prng_files/vba_prng.html)
**Important Notes**:
- Returns values in range [0, 1) - includes 0, excludes 1
- Same seed produces same sequence (deterministic)
- Use `Randomize` statement to initialize random number generator with time-based seed
- Without `Randomize`, same sequence is generated each program run
- Passing negative number sets seed for reproducible sequences
- Passing 0 returns last generated number (useful for debugging)
**Common Usage Pattern**:</code></pre>
<p>vb
Randomize ' Initialize with time-based seed
x = Rnd ' Get random number 0 <= x < 1</p>
<pre><code>**Generating Random Integers**:</code></pre>
<p>vb
' Random integer from min to max (inclusive)
randomInt = Int((max - min + 1) * Rnd + min)</p>
<pre><code>**Seeding for Reproducibility**:</code></pre>
<p>vb
Rnd -1 ' Reset to use seed
Randomize seed ' Set specific seed
x = Rnd ' Get first number in sequence</p>
<pre><code>## Typical Uses
1. **Games**: Generate random positions, events, or outcomes
2. **Simulations**: Create random data for Monte Carlo simulations
3. **Testing**: Generate random test data
4. **Sampling**: Random selection from datasets
5. **Shuffling**: Randomize order of items
6. **Password Generation**: Create random character sequences
7. **Animation**: Random movement or effects
8. **Data Masking**: Generate random replacement values
## Basic Examples
### Example 1: Simple Random Number</code></pre>
<p>vb
' Generate random number between 0 and 1
Randomize
Dim randomValue As Single
randomValue = Rnd() ' e.g., 0.7234567</p>
<pre><code>### Example 2: Random Integer in Range</code></pre>
<p>vb
' Generate random integer from 1 to 100
Randomize
Dim randomInt As Integer
randomInt = Int(Rnd * 100) + 1</p>
<pre><code>### Example 3: Random Dice Roll</code></pre>
<p>vb
' Simulate rolling a six-sided die
Randomize
Dim diceRoll As Integer
diceRoll = Int(Rnd * 6) + 1 ' Returns 1-6</p>
<pre><code>### Example 4: Random Selection</code></pre>
<p>vb
' Select random item from array
Randomize
Dim items(5) As String
Dim randomIndex As Integer
items(0) = "Apple"
items(1) = "Banana"
items(2) = "Cherry"
items(3) = "Date"
items(4) = "Elderberry"
randomIndex = Int(Rnd * 5)
MsgBox "Selected: " & items(randomIndex)</p>
<pre><code>## Common Patterns
### Pattern 1: `RandomInteger`</code></pre>
<p>vb
Function RandomInteger(minValue As Long, maxValue As Long) As Long
' Generate random integer in range [minValue, maxValue]
RandomInteger = Int((maxValue - minValue + 1) * Rnd + minValue)
End Function</p>
<pre><code>### Pattern 2: `RandomDouble`</code></pre>
<p>vb
Function RandomDouble(minValue As Double, maxValue As Double) As Double
' Generate random double in range [minValue, maxValue)
RandomDouble = (maxValue - minValue) * Rnd + minValue
End Function</p>
<pre><code>### Pattern 3: `RandomBoolean`</code></pre>
<p>vb
Function RandomBoolean() As Boolean
' Generate random True/False
RandomBoolean = (Rnd >= 0.5)
End Function</p>
<pre><code>### Pattern 4: `RandomChoice`</code></pre>
<p>vb
Function RandomChoice(items As Variant) As Variant
' Select random item from array
Dim index As Integer
If Not IsArray(items) Then
Err.Raise 5, , "Parameter must be an array"
End If
index = Int(Rnd * (UBound(items) - LBound(items) + 1)) + LBound(items)
RandomChoice = items(index)
End Function</p>
<pre><code>### Pattern 5: `ShuffleArray`</code></pre>
<p>vb
Sub ShuffleArray(arr As Variant)
' Randomly shuffle array elements (Fisher-Yates algorithm)
Dim i As Integer
Dim j As Integer
Dim temp As Variant
For i = UBound(arr) To LBound(arr) + 1 Step -1
j = Int(Rnd * (i - LBound(arr) + 1)) + LBound(arr)
temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
Next i
End Sub</p>
<pre><code>### Pattern 6: `RandomString`</code></pre>
<p>vb
Function RandomString(length As Integer, _
Optional chars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") As String
' Generate random string of specified length
Dim i As Integer
Dim result As String
Dim index As Integer
result = ""
For i = 1 To length
index = Int(Rnd * Len(chars)) + 1
result = result & Mid(chars, index, 1)
Next i
RandomString = result
End Function</p>
<pre><code>### Pattern 7: `RandomColor`</code></pre>
<p>vb
Function RandomColor() As Long
' Generate random RGB color
Dim r As Integer
Dim g As Integer
Dim b As Integer
r = Int(Rnd * 256)
g = Int(Rnd * 256)
b = Int(Rnd * 256)
RandomColor = RGB(r, g, b)
End Function</p>
<pre><code>### Pattern 8: `RandomPercentage`</code></pre>
<p>vb
Function RandomPercentage(probability As Double) As Boolean
' Return True with specified probability (0.0 to 1.0)
RandomPercentage = (Rnd < probability)
End Function</p>
<pre><code>### Pattern 9: `RandomDate`</code></pre>
<p>vb
Function RandomDate(startDate As Date, endDate As Date) As Date
' Generate random date between start and end dates
Dim daysDiff As Long
Dim randomDays As Long
daysDiff = DateDiff("d", startDate, endDate)
randomDays = Int(Rnd * (daysDiff + 1))
RandomDate = DateAdd("d", randomDays, startDate)
End Function</p>
<pre><code>### Pattern 10: `WeightedRandom`</code></pre>
<p>vb
Function WeightedRandom(weights() As Double) As Integer
' Select index based on weighted probabilities
Dim total As Double
Dim i As Integer
Dim randomValue As Double
Dim cumulative As Double
' Calculate total weight
total = 0
For i = LBound(weights) To UBound(weights)
total = total + weights(i)
Next i
' Generate random value
randomValue = Rnd * total
' Find corresponding index
cumulative = 0
For i = LBound(weights) To UBound(weights)
cumulative = cumulative + weights(i)
If randomValue < cumulative Then
WeightedRandom = i
Exit Function
End If
Next i
WeightedRandom = UBound(weights)
End Function</p>
<pre><code>## Advanced Usage
### Example 1: Random Number Generator Class</code></pre>
<p>vb
' Comprehensive random number generator with multiple distributions
Class RandomGenerator
Private m_seed As Long
Private m_seeded As Boolean
Public Sub Initialize(Optional seed As Long = 0)
If seed <> 0 Then
m_seed = seed
Rnd -1
Randomize seed
m_seeded = True
Else
Randomize
m_seeded = False
End If
End Sub
Public Function NextDouble() As Double
' Get next random double [0, 1)
NextDouble = Rnd
End Function
Public Function NextInteger(minValue As Long, maxValue As Long) As Long
' Get random integer [minValue, maxValue]
If minValue > maxValue Then
Err.Raise 5, , "minValue must be <= maxValue"
End If
NextInteger = Int((maxValue - minValue + 1) * Rnd + minValue)
End Function
Public Function NextBoolean() As Boolean
' Get random boolean
NextBoolean = (Rnd >= 0.5)
End Function
Public Function NextGaussian(Optional mean As Double = 0, _
Optional stdDev As Double = 1) As Double
' Generate Gaussian (normal) distribution using Box-Muller transform
Dim u1 As Double, u2 As Double
Dim z0 As Double
u1 = Rnd
u2 = Rnd
' Box-Muller transform
z0 = Sqr(-2 * Log(u1)) * Cos(2 * 3.14159265358979 * u2)
NextGaussian = mean + stdDev * z0
End Function
Public Function NextBytes(count As Long) As Byte()
' Generate array of random bytes
Dim bytes() As Byte
Dim i As Long
ReDim bytes(0 To count - 1)
For i = 0 To count - 1
bytes(i) = Int(Rnd * 256)
Next i
NextBytes = bytes
End Function
Public Sub Reset()
' Reset to original seed if seeded
If m_seeded Then
Rnd -1
Randomize m_seed
Else
Randomize
End If
End Sub
Public Function GetSeed() As Long
GetSeed = m_seed
End Function
End Class</p>
<pre><code>### Example 2: Monte Carlo Simulator</code></pre>
<p>vb
' Monte Carlo simulation for estimating probabilities
Module MonteCarloSimulator
Public Function EstimatePi(iterations As Long) As Double
' Estimate Pi using Monte Carlo method
Dim insideCircle As Long
Dim i As Long
Dim x As Double, y As Double
Randomize
insideCircle = 0
For i = 1 To iterations
x = Rnd
y = Rnd
If (x * x + y * y) < 1 Then
insideCircle = insideCircle + 1
End If
Next i
EstimatePi = 4 * (insideCircle / iterations)
End Function
Public Function SimulateDiceRolls(numDice As Integer, _
numRolls As Long) As Long()
' Simulate rolling multiple dice and return frequency distribution
Dim results() As Long
Dim minSum As Integer, maxSum As Integer
Dim i As Long, j As Integer
Dim sum As Integer
minSum = numDice
maxSum = numDice * 6
ReDim results(minSum To maxSum)
Randomize
For i = 1 To numRolls
sum = 0
For j = 1 To numDice
sum = sum + Int(Rnd * 6) + 1
Next j
results(sum) = results(sum) + 1
Next i
SimulateDiceRolls = results
End Function
Public Function SimulateStockPrice(startPrice As Double, _
days As Long, _
volatility As Double, _
drift As Double) As Double()
' Simulate stock price using geometric Brownian motion
Dim prices() As Double
Dim i As Long
Dim randomShock As Double
ReDim prices(0 To days)
prices(0) = startPrice
Randomize
For i = 1 To days
randomShock = (Rnd - 0.5) * 2 ' -1 to 1
prices(i) = prices(i - 1) * (1 + drift + volatility * randomShock)
Next i
SimulateStockPrice = prices
End Function
End Module</p>
<pre><code>### Example 3: Random Data Generator</code></pre>
<p>vb
' Generate random test data for various purposes
Class RandomDataGenerator
Private m_firstNames() As String
Private m_lastNames() As String
Private m_emailDomains() As String
Public Sub Initialize()
Randomize
m_firstNames = Array("John", "Jane", "Michael", "Sarah", "David", _
"Emily", "James", "Emma", "Robert", "Lisa")
m_lastNames = Array("Smith", "Johnson", "Williams", "Brown", "Jones", _
"Garcia", "Miller", "Davis", "Rodriguez", "Martinez")
m_emailDomains = Array("gmail.com", "yahoo.com", "hotmail.com", _
"outlook.com", "example.com")
End Sub
Public Function GenerateName() As String
' Generate random full name
Dim firstName As String
Dim lastName As String
firstName = m_firstNames(Int(Rnd * (UBound(m_firstNames) + 1)))
lastName = m_lastNames(Int(Rnd * (UBound(m_lastNames) + 1)))
GenerateName = firstName & " " & lastName
End Function
Public Function GenerateEmail(Optional name As String = "") As String
' Generate random email address
Dim localPart As String
Dim domain As String
If name = "" Then
localPart = "user" & Int(Rnd * 10000)
Else
localPart = LCase(Replace(name, " ", "."))
End If
domain = m_emailDomains(Int(Rnd * (UBound(m_emailDomains) + 1)))
GenerateEmail = localPart & "@" & domain
End Function
Public Function GeneratePhoneNumber() As String
' Generate random US phone number
Dim areaCode As String
Dim exchange As String
Dim number As String
areaCode = Format(Int(Rnd * 900) + 100, "000")
exchange = Format(Int(Rnd * 900) + 100, "000")
number = Format(Int(Rnd * 10000), "0000")
GeneratePhoneNumber = "(" & areaCode & ") " & exchange & "-" & number
End Function
Public Function GenerateAddress() As String
' Generate random street address
Dim streetNumber As String
Dim streetNames() As String
Dim streetName As String
Dim streetTypes() As String
Dim streetType As String
streetNames = Array("Main", "Oak", "Maple", "Cedar", "Elm", _
"Washington", "Park", "Lake", "Hill", "Pine")
streetTypes = Array("St", "Ave", "Blvd", "Rd", "Ln", "Dr")
streetNumber = Int(Rnd * 9900) + 100
streetName = streetNames(Int(Rnd * (UBound(streetNames) + 1)))
streetType = streetTypes(Int(Rnd * (UBound(streetTypes) + 1)))
GenerateAddress = streetNumber & " " & streetName & " " & streetType
End Function
Public Function GeneratePassword(length As Integer) As String
' Generate random password with mixed characters
Dim chars As String
Dim i As Integer
Dim result As String
Dim index As Integer
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*"
result = ""
For i = 1 To length
index = Int(Rnd * Len(chars)) + 1
result = result & Mid(chars, index, 1)
Next i
GeneratePassword = result
End Function
End Class</p>
<pre><code>### Example 4: Card Deck Shuffler</code></pre>
<p>vb
' Simulate a deck of cards with shuffling
Class CardDeck
Private Type Card
Suit As String
Rank As String
Value As Integer
End Type
Private m_cards() As Card
Private m_currentCard As Integer
Public Sub Initialize()
Dim suits() As String
Dim ranks() As String
Dim i As Integer, j As Integer
Dim cardIndex As Integer
suits = Array("Hearts", "Diamonds", "Clubs", "Spades")
ranks = Array("2", "3", "4", "5", "6", "7", "8", "9", "10", _
"Jack", "Queen", "King", "Ace")
ReDim m_cards(0 To 51)
cardIndex = 0
For i = 0 To 3
For j = 0 To 12
m_cards(cardIndex).Suit = suits(i)
m_cards(cardIndex).Rank = ranks(j)
m_cards(cardIndex).Value = j + 2
cardIndex = cardIndex + 1
Next j
Next i
m_currentCard = 0
End Sub
Public Sub Shuffle()
' Shuffle deck using Fisher-Yates algorithm
Dim i As Integer
Dim j As Integer
Dim temp As Card
Randomize
For i = 51 To 1 Step -1
j = Int(Rnd * (i + 1))
temp = m_cards(i)
m_cards(i) = m_cards(j)
m_cards(j) = temp
Next i
m_currentCard = 0
End Sub
Public Function DrawCard() As String
' Draw next card from deck
Dim card As Card
If m_currentCard > 51 Then
DrawCard = "No cards left"
Exit Function
End If
card = m_cards(m_currentCard)
m_currentCard = m_currentCard + 1
DrawCard = card.Rank & " of " & card.Suit
End Function
Public Function CardsRemaining() As Integer
CardsRemaining = 52 - m_currentCard
End Function
Public Sub Reset()
m_currentCard = 0
End Sub
End Class</p>
<pre><code>## Error Handling
The `Rnd` function rarely generates errors, but there are some considerations:
**Type Mismatch (Error 13)**:
- Occurs if the optional number parameter cannot be converted to numeric type
Example error handling:</code></pre>
<p>vb
On Error Resume Next
Dim randomValue As Single
randomValue = Rnd(userInput)
If Err.Number <> 0 Then
MsgBox "Invalid seed value"
randomValue = Rnd ' Use default behavior
End If
On Error GoTo 0
```</p>
<h2 id="performance-considerations">Performance Considerations</h2>
<ul>
<li><code>Rnd</code> is very fast - can generate millions of numbers per second</li>
<li>Not cryptographically secure - don't use for security purposes</li>
<li>Same seed produces same sequence (deterministic)</li>
<li>For better randomness, call <code>Randomize</code> at program start</li>
<li>Calling <code>Randomize</code> frequently can reduce randomness quality</li>
<li>Consider caching random values if generating many at once</li>
</ul>
<h2 id="best-practices">Best Practices</h2>
<ol>
<li><strong>Always Randomize</strong>: Call <code>Randomize</code> once at program start for non-deterministic behavior</li>
<li><strong>Use Seed for Testing</strong>: Use fixed seed (negative number) for reproducible test cases</li>
<li><strong>Validate Ranges</strong>: Check min/max values when generating random integers</li>
<li><strong>Avoid Modulo Bias</strong>: Use <code>Int(Rnd * n)</code> not <code>Rnd Mod n</code> for uniform distribution</li>
<li><strong>Don't Use for Security</strong>: Not suitable for passwords, encryption keys, or security tokens</li>
<li><strong>Cache Rnd Values</strong>: If generating many values, avoid repeated function calls overhead</li>
<li><strong>Document Seed Usage</strong>: Clearly document when using fixed seeds for reproducibility</li>
<li><strong>Test Edge Cases</strong>: Verify behavior at range boundaries (min, max values)</li>
<li><strong>Use Helper Functions</strong>: Wrap Rnd in helper functions for cleaner code</li>
<li><strong>Consider Distribution</strong>: Understand that Rnd provides uniform distribution</li>
</ol>
<h2 id="comparison-with-related-functions">Comparison with Related Functions</h2>
<table>
<thead>
<tr>
<th>Function/Statement</th>
<th>Purpose</th>
<th>Returns</th>
<th>Use Case</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Rnd</strong></td>
<td>Random number</td>
<td>Single [0, 1)</td>
<td>Generate random values</td>
</tr>
<tr>
<td><strong>Randomize</strong></td>
<td>Initialize RNG</td>
<td>Nothing (statement)</td>
<td>Seed random number generator</td>
</tr>
<tr>
<td><strong>Int</strong></td>
<td>Integer part</td>
<td>Integer</td>
<td>Convert Rnd to integer range</td>
</tr>
<tr>
<td><strong>Timer</strong></td>
<td>Elapsed seconds</td>
<td>Single</td>
<td>Often used with Randomize</td>
</tr>
</tbody>
</table>
<h2 id="platform-and-version-notes">Platform and Version Notes</h2>
<ul>
<li>Available in all versions of VB6 and VBA</li>
<li>Uses linear congruential generator (LCG) algorithm</li>
<li>Not cryptographically secure</li>
<li>Sequence repeats after approximately 16 million numbers</li>
<li>In VB.NET, replaced by <code>System.Random</code> class</li>
<li><code>Randomize</code> uses <code>Timer</code> function by default if no seed provided</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li>Not cryptographically secure (predictable sequence)</li>
<li>Limited period (sequence repeats after ~16M values)</li>
<li>No built-in support for other distributions (normal, exponential, etc.)</li>
<li>Cannot generate random integers directly (requires Int/Floor conversion)</li>
<li>Thread safety not guaranteed in multi-threaded scenarios</li>
<li>Quality lower than modern RNGs (Mersenne Twister, etc.)</li>
</ul>
<h2 id="related-functions">Related Functions</h2>
<ul>
<li><code>Randomize</code>: Initializes the random number generator with a seed</li>
<li><code>Int</code>: Returns the integer portion of a number (used to convert Rnd to integer range)</li>
<li><code>Timer</code>: Returns seconds since midnight (often used with Randomize)</li>
</ul>
</article>
<div style="margin-top: 3rem; padding-top: 2rem; border-top: 1px solid var(--border-color);">
<p>
<a href="index.html">← Back to Math</a> |
<a href="../index.html">View all functions</a>
</p>
</div>
</main>
<footer>
<div class="container">
<p>© 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
</div>
</footer>
</body>
</html>