Guest User

VBA Needle in the Haystack Testing Module

a guest
Nov 16th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.23 KB | None | 0 0
  1. Option Compare Binary
  2. Option Explicit
  3. Option Base 0
  4.  
  5. ''##############################################################################
  6. ''
  7. ''  @Author: Taylor Scott
  8. ''  @Date:   16 Nov 2019
  9. ''
  10. ''##############################################################################
  11.  
  12.  
  13. ''##############################################################################
  14. ''
  15. ''
  16. ''                                   WARNING
  17. ''
  18. ''
  19. ''  Do not run the test method or setup method included herein in a document
  20. ''  that contains important or sensitive data. All data on the Activesheet at
  21. ''  the time of execution shall be cleared and may not be recovered.
  22. ''
  23. ''
  24. ''##############################################################################
  25.  
  26.  
  27. ''  test function for needle in haystack algorithms in the vain of
  28. ''
  29. ''      https://codegolf.stackexchange.com/questions/194352/make-a-haystack-with-a-needle
  30. ''
  31. ''  specifically, algorithms formualted as by @EngineerToast's answer
  32. ''
  33. ''      https://codegolf.stackexchange.com/a/194373/61846
  34. ''
  35.  
  36.  
  37. ''  From testing using this sub, it is recommended that the subroutine be
  38. ''  changed to
  39. ''
  40. ''
  41. ''      Sub n(w,h,x,y)
  42. ''      a=94*Rnd
  43. ''      [A1].Resize(h,w)=Chr(a+32)
  44. ''      Cells(y,x)=Chr(32+(a+93*Rnd+1)Mod 95)
  45. ''      End Sub
  46. ''
  47. ''  as the current verison of the subrountine does not capture all possible
  48. ''  combinations of needle and haystack
  49.  
  50. Sub test_randomness()
  51.    
  52.     Dim count(32 To 126, 32 To 126) As Long ''  initialize an array to hold count of the
  53.                                             ''  instances of given combinations of A and B
  54.    
  55.     Dim a As Byte       ''  the first ascii character num  - the haystack
  56.     Dim b As Byte       ''  the second ascii character num - the needle
  57.     Dim k As Byte       ''  dummy var for an alternate definition of a (k=a-32)
  58.     Dim r As Double     ''  dummy var that allows for switching between random
  59.                         ''      and edge case testing
  60.     Dim iter As Long    ''  Iteration counter
  61.    
  62.     Call prep_output    '' prep the table
  63.     Call Randomize      '' intitialize RND
  64.    
  65.     For a = 32 To 126   '' iterate over the possible values of A
  66.         For iter = 1 To 10 ^ 5  ''  iterate over that value 10^5 times to get good
  67.                                 ''  sample size out of the random values
  68.            
  69.             ''''''''''''''''''''''''''''''
  70.             '' Original Function
  71.             ''''''''''''''''''''''''''''''
  72.             'b=(a+62*Rnd)Mod 95+32      ''<- Uncomment this line and comment out the lines
  73.                                         ''   below that define k and R and B to test
  74.                                         ''   the function as it originally appeared
  75.            
  76.             ''  Compare against
  77.            
  78.            
  79.             ''''''''''''''''''''''''''''''
  80.             ''  New Function
  81.             ''''''''''''''''''''''''''''''
  82.             k = a - 32                  '' move the `+32` into the Chr statement
  83.             'r = -(iter Mod 2 = 0)      '' edge case testing. Alternating R=0s and R=1s
  84.             r = Rnd                     '' randomness test
  85.             b = 32 + (k + 93 * r + 1) Mod 95
  86.            
  87.             ''  from edge case testing, r does not need to go to 0 or 1 to cover all possible
  88.             ''  values, as the Mod operator introduces rounding. ie if r> 0.9947 the value of b
  89.             ''  will be rounded up to the maximum possible value just before the mod operation
  90.            
  91.            
  92.             'PPCG style explaination of how this works
  93.             'b=32+(a+93*Rnd+1)Mod 95
  94.             'b=                       Define the ASCII char number of the needle as
  95.             '  32+                    32 plus
  96.             '     (          )Mod 95  the modulus with respect to 95 of
  97.             '      a                  the ASCII number of the hay, minus 32
  98.             '       +93*Rnd           Plus a random shift of between 0 and 93 units
  99.             '              +1         Plus a single unit garunteed shift
  100.            
  101.            
  102.             '' Keep count of the total number of occurances of B, given a
  103.             Let count(a, b) = count(a, b) + 1
  104.         Next iter
  105.        
  106.         '' Dump the counts into the table
  107.         Let [D4].Resize(95, 95) = count
  108.        
  109.         '' GarbageHandling
  110.         Call VBA.DoEvents
  111.    
  112.     Next a
  113. End Sub
  114.  
  115.  
  116. ''  Prepares a table with the ASCII character and the associated numbers on
  117. ''  on both the x and y axises
  118. Private Sub prep_output()
  119.  
  120.     Dim a As Byte, b As Byte, i As Byte '' byte iterators
  121.  
  122.     '' turn off screen updates to increase execution speed
  123.     Let Application.ScreenUpdating = False
  124.    
  125.     ''  clear old data
  126.     Call Cells.Clear
  127.     Let Cells.Style = "Normal"
  128.    
  129.     ''  add in the A and B value axises
  130.     Let [A1].Resize(95, 2) = Array("=row()+28", "=char(A1)")
  131.     Call [1:2].Insert
  132.     Let [C1].Resize(1, 95) = "=column()+28"
  133.     Let [C2].Resize(1, 95) = "=Char(C1)"
  134.    
  135.     '' add common axis legends
  136.     Let [A1] = "ASCII"
  137.     Let [B2] = "Char"
  138.    
  139.     ''  add axis title spaces
  140.     Call [1:1].Insert
  141.     Call [A:A].Insert
  142.     Let [A:C,1:3].Font.Bold = True
  143.     Let [A:A].ColumnWidth = 3
  144.     Let [B:C].ColumnWidth = 5
  145.    
  146.     ''  define A value axis title
  147.     Call [A4].Resize(95).Merge
  148.     Let [A4] = "A value"
  149.     Let [A4].Orientation = 90
  150.     Let [A4].Style = "Heading 1"
  151.     Let [A4].VerticalAlignment = xlCenter
  152.     Let [A:C].HorizontalAlignment = xlCenter
  153.    
  154.     ''  define B value axis title
  155.     Call [D1].Resize(1, 95).Merge
  156.     Let [D1] = "B value"
  157.     Let [D1].Style = "Heading 1"
  158.     Let [D1].Resize(1, 95).EntireColumn.ColumnWidth = 4.25
  159.     Let [1:3].HorizontalAlignment = xlCenter
  160.    
  161.     ''  set bulk coloration
  162.     Let [B2].Resize(97, 97).Style = "Accent1"
  163.     Let [D4].Resize(95, 95).Style = "20% - Accent1"
  164.    
  165.     ''  set accent colors for top left corner
  166.     Let [A1:A3,B1:C1].Interior.Color = rgbWhite
  167.     Let [C2,B3].Interior.Color = 13602409   ' GetAccentAtPercentage("Accent1", 80)
  168.                                         '' (custom function - AccentPercentages Module)
  169.    
  170.     ''  set style of every other column and row to be slightly darker
  171.     ''  than bulk coloration
  172.     For i = 33 To 126 Step 2
  173.         Let [D4].Offset(i - 32, 0).Resize(1, 95).Style = "40% - Accent1"
  174.         Let [D4].Offset(0, i - 32).Resize(95, 1).Style = "40% - Accent1"
  175.     Next i
  176.    
  177.     ''  set the points where the darker areas overlap to be darker still
  178.     For a = 33 To 126 Step 2
  179.         For b = 33 To 126 Step 2
  180.             Let [D4].Offset(a - 32, b - 32).Style = "60% - Accent1"
  181.         Next b
  182.     Next a
  183.    
  184.     ''  set the diagonal of the identity matrix to be dark gray, as any
  185.     ''  value which falls in the region indicates that the test function
  186.     ''  for b has failed
  187.     For a = 32 To 126
  188.         Let [D4].Offset(a - 32, a - 32).Style = "Accent3"
  189.     Next a
  190.    
  191.     '' add in borders
  192.     Let [D4].Resize(95, 95).Borders.Weight = xlThin
  193.     Call [D4].Resize(95, 95).BorderAround(Weight:=xlMedium)
  194.     Call [B2].Resize(2, 2).BorderAround(Weight:=xlThin)
  195.     Call [A1].Resize(98, 98).BorderAround(Weight:=xlThick)
  196.  
  197.     '' turn back on screen updates
  198.     Let Application.ScreenUpdating = True
  199. End Sub
Add Comment
Please, Sign In to add comment