vb6parse 1.0.1

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
<!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>&lt; 0</td>
<td>Same number every time, using <code>number</code> as the seed</td>
</tr>
<tr>
<td>&gt; 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 &lt;= x &lt; 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: " &amp; 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 &gt;= 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 &amp; 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 &lt; 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 &lt; 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 &lt;&gt; 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 &gt; maxValue Then
            Err.Raise 5, , "minValue must be &lt;= maxValue"
        End If
        NextInteger = Int((maxValue - minValue + 1) * Rnd + minValue)
    End Function
    Public Function NextBoolean() As Boolean
        ' Get random boolean
        NextBoolean = (Rnd &gt;= 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) &lt; 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 &amp; " " &amp; 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" &amp; Int(Rnd * 10000)
        Else
            localPart = LCase(Replace(name, " ", "."))
        End If
        domain = m_emailDomains(Int(Rnd * (UBound(m_emailDomains) + 1)))
        GenerateEmail = localPart &amp; "@" &amp; 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 = "(" &amp; areaCode &amp; ") " &amp; exchange &amp; "-" &amp; 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 &amp; " " &amp; streetName &amp; " " &amp; 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!@#$%^&amp;*"
        result = ""
        For i = 1 To length
            index = Int(Rnd * Len(chars)) + 1
            result = result &amp; 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 &gt; 51 Then
            DrawCard = "No cards left"
            Exit Function
        End If
        card = m_cards(m_currentCard)
        m_currentCard = m_currentCard + 1
        DrawCard = card.Rank &amp; " of " &amp; 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 &lt;&gt; 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>&copy; 2024-2026 VB6Parse Contributors. Licensed under the MIT License.</p>
        </div>
    </footer>
</body>
</html>