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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
//! # Rnd Function
//!
//! Returns a Single containing a pseudo-random number.
//!
//! ## Syntax
//!
//! ```vb
//! Rnd[(number)]
//! ```
//!
//! ## Parameters
//!
//! - `number` - Optional. Single or any valid numeric expression.
//!
//! ## Return Value
//!
//! Returns a `Single` value greater than or equal to 0 and less than 1.
//!
//! The behavior depends on the `number` argument:
//!
//! | Number Value | Result |
//! |--------------|--------|
//! | < 0 | Same number every time, using `number` as the seed |
//! | > 0 | Next random number in sequence |
//! | = 0 | Most recently generated number |
//! | Omitted | Next random number in sequence |
//!
//! ## Remarks
//!
//! The `Rnd` 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```
//!
//! [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**:
//! ```vb
//! Randomize               ' Initialize with time-based seed
//! x = Rnd                 ' Get random number 0 <= x < 1
//! ```
//!
//! **Generating Random Integers**:
//! ```vb
//! ' Random integer from min to max (inclusive)
//! randomInt = Int((max - min + 1) * Rnd + min)
//! ```
//!
//! **Seeding for Reproducibility**:
//! ```vb
//! Rnd -1                  ' Reset to use seed
//! Randomize seed          ' Set specific seed
//! x = Rnd                 ' Get first number in sequence
//! ```
//!
//! ## 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
//! ```vb
//! ' Generate random number between 0 and 1
//! Randomize
//! Dim randomValue As Single
//! randomValue = Rnd()  ' e.g., 0.7234567
//! ```
//!
//! ### Example 2: Random Integer in Range
//! ```vb
//! ' Generate random integer from 1 to 100
//! Randomize
//! Dim randomInt As Integer
//! randomInt = Int(Rnd * 100) + 1
//! ```
//!
//! ### Example 3: Random Dice Roll
//! ```vb
//! ' Simulate rolling a six-sided die
//! Randomize
//! Dim diceRoll As Integer
//! diceRoll = Int(Rnd * 6) + 1  ' Returns 1-6
//! ```
//!
//! ### Example 4: Random Selection
//! ```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)
//! ```
//!
//! ## Common Patterns
//!
//! ### Pattern 1: `RandomInteger`
//! ```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
//! ```
//!
//! ### Pattern 2: `RandomDouble`
//! ```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
//! ```
//!
//! ### Pattern 3: `RandomBoolean`
//! ```vb
//! Function RandomBoolean() As Boolean
//!     ' Generate random True/False
//!     RandomBoolean = (Rnd >= 0.5)
//! End Function
//! ```
//!
//! ### Pattern 4: `RandomChoice`
//! ```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
//! ```
//!
//! ### Pattern 5: `ShuffleArray`
//! ```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
//! ```
//!
//! ### Pattern 6: `RandomString`
//! ```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
//! ```
//!
//! ### Pattern 7: `RandomColor`
//! ```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
//! ```
//!
//! ### Pattern 8: `RandomPercentage`
//! ```vb
//! Function RandomPercentage(probability As Double) As Boolean
//!     ' Return True with specified probability (0.0 to 1.0)
//!     RandomPercentage = (Rnd < probability)
//! End Function
//! ```
//!
//! ### Pattern 9: `RandomDate`
//! ```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
//! ```
//!
//! ### Pattern 10: `WeightedRandom`
//! ```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
//! ```
//!
//! ## Advanced Usage
//!
//! ### Example 1: Random Number Generator Class
//! ```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
//! ```
//!
//! ### Example 2: Monte Carlo Simulator
//! ```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
//! ```
//!
//! ### Example 3: Random Data Generator
//! ```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
//! ```
//!
//! ### Example 4: Card Deck Shuffler
//! ```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
//! ```
//!
//! ## 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:
//!
//! ```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
//! ```
//!
//! ## Performance Considerations
//!
//! - `Rnd` is very fast - can generate millions of numbers per second
//! - Not cryptographically secure - don't use for security purposes
//! - Same seed produces same sequence (deterministic)
//! - For better randomness, call `Randomize` at program start
//! - Calling `Randomize` frequently can reduce randomness quality
//! - Consider caching random values if generating many at once
//!
//! ## Best Practices
//!
//! 1. **Always Randomize**: Call `Randomize` once at program start for non-deterministic behavior
//! 2. **Use Seed for Testing**: Use fixed seed (negative number) for reproducible test cases
//! 3. **Validate Ranges**: Check min/max values when generating random integers
//! 4. **Avoid Modulo Bias**: Use `Int(Rnd * n)` not `Rnd Mod n` for uniform distribution
//! 5. **Don't Use for Security**: Not suitable for passwords, encryption keys, or security tokens
//! 6. **Cache Rnd Values**: If generating many values, avoid repeated function calls overhead
//! 7. **Document Seed Usage**: Clearly document when using fixed seeds for reproducibility
//! 8. **Test Edge Cases**: Verify behavior at range boundaries (min, max values)
//! 9. **Use Helper Functions**: Wrap Rnd in helper functions for cleaner code
//! 10. **Consider Distribution**: Understand that Rnd provides uniform distribution
//!
//! ## Comparison with Related Functions
//!
//! | Function/Statement | Purpose | Returns | Use Case |
//! |-------------------|---------|---------|----------|
//! | **Rnd** | Random number | Single [0, 1) | Generate random values |
//! | **Randomize** | Initialize RNG | Nothing (statement) | Seed random number generator |
//! | **Int** | Integer part | Integer | Convert Rnd to integer range |
//! | **Timer** | Elapsed seconds | Single | Often used with Randomize |
//!
//! ## Platform and Version Notes
//!
//! - Available in all versions of VB6 and VBA
//! - Uses linear congruential generator (LCG) algorithm
//! - Not cryptographically secure
//! - Sequence repeats after approximately 16 million numbers
//! - In VB.NET, replaced by `System.Random` class
//! - `Randomize` uses `Timer` function by default if no seed provided
//!
//! ## Limitations
//!
//! - Not cryptographically secure (predictable sequence)
//! - Limited period (sequence repeats after ~16M values)
//! - No built-in support for other distributions (normal, exponential, etc.)
//! - Cannot generate random integers directly (requires Int/Floor conversion)
//! - Thread safety not guaranteed in multi-threaded scenarios
//! - Quality lower than modern RNGs (Mersenne Twister, etc.)
//!
//! ## Related Functions
//!
//! - `Randomize`: Initializes the random number generator with a seed
//! - `Int`: Returns the integer portion of a number (used to convert Rnd to integer range)
//! - `Timer`: Returns seconds since midnight (often used with Randomize)

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn rnd_basic() {
        let source = r"
Dim randomValue As Single
randomValue = Rnd()
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_no_parens() {
        let source = r"
Dim x As Single
x = Rnd
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_with_argument() {
        let source = r"
Dim result As Single
result = Rnd(-1)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_if_statement() {
        let source = r#"
If Rnd > 0.5 Then
    MsgBox "Heads"
Else
    MsgBox "Tails"
End If
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_function_return() {
        let source = r"
Function GetRandomValue() As Single
    GetRandomValue = Rnd
End Function
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_variable_assignment() {
        let source = r"
Dim randomNum As Single
randomNum = Rnd
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_msgbox() {
        let source = r#"
MsgBox "Random: " & Rnd
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_debug_print() {
        let source = r"
Debug.Print Rnd()
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_select_case() {
        let source = r#"
Dim value As Single
value = Rnd
Select Case value
    Case Is < 0.33
        result = "Low"
    Case Is < 0.67
        result = "Medium"
    Case Else
        result = "High"
End Select
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_class_usage() {
        let source = r"
Private m_randomValue As Single

Public Sub GenerateRandom()
    m_randomValue = Rnd
End Sub
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_with_statement() {
        let source = r"
With dataObject
    .RandomValue = Rnd
End With
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_elseif() {
        let source = r"
Dim r As Single
r = Rnd
If r < 0.25 Then
    category = 1
ElseIf r < 0.5 Then
    category = 2
End If
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_for_loop() {
        let source = r"
For i = 1 To 10
    randomNumbers(i) = Rnd
Next i
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_do_while() {
        let source = r"
Do While Rnd < 0.95
    count = count + 1
Loop
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_do_until() {
        let source = r"
Do Until Rnd > 0.9
    attempts = attempts + 1
Loop
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_while_wend() {
        let source = r"
While Rnd < 0.8
    iterations = iterations + 1
Wend
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_parentheses() {
        let source = r"
Dim val As Single
val = (Rnd)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_iif() {
        let source = r#"
Dim result As String
result = IIf(Rnd > 0.5, "Win", "Lose")
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_array_assignment() {
        let source = r"
Dim values(10) As Single
values(i) = Rnd
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_property_assignment() {
        let source = r"
Set obj = New RandomData
obj.Value = Rnd
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_function_argument() {
        let source = r"
Call ProcessValue(Rnd)
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_concatenation() {
        let source = r#"
Dim msg As String
msg = "Value: " & Rnd
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_with_int() {
        let source = r"
Dim diceRoll As Integer
diceRoll = Int(Rnd * 6) + 1
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_comparison() {
        let source = r#"
If Rnd < 0.3 Then
    status = "Rare"
End If
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_multiple_calls() {
        let source = r"
Dim x As Single, y As Single
x = Rnd
y = Rnd
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_error_handling() {
        let source = r"
On Error Resume Next
Dim randomVal As Single
randomVal = Rnd
If Err.Number <> 0 Then
    randomVal = 0.5
End If
";
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }

    #[test]
    fn rnd_on_error_goto() {
        let source = r#"
Sub GenerateRandom()
    On Error GoTo ErrorHandler
    Dim r As Single
    r = Rnd
    Exit Sub
ErrorHandler:
    MsgBox "Error generating random number"
End Sub
"#;
        let (cst_opt, _failures) = ConcreteSyntaxTree::from_text("test.bas", source).unpack();
        let cst = cst_opt.expect("CST should be parsed");

        let tree = cst.to_serializable();

        let mut settings = insta::Settings::clone_current();
        settings.set_snapshot_path("../../../../../snapshots/syntax/library/functions/math/rnd");
        settings.set_prepend_module_to_snapshot(false);
        let _guard = settings.bind_to_scope();
        insta::assert_yaml_snapshot!(tree);
    }
}