Excel randomly generates data 2

Source: Internet
Author: User
Tags random seed repetition

2 million takes approximately 10 seconds to output the result to a TXT file.

  1. Sub GetPassword () ' by Kagawa
  2. Dim I&, J&, K&, L&, M&, N&, R&, S $, s1$, s2$, t$, C1&, C2&, Cnt&, tms#
  3. TMS = Timer
  4. m = ActiveCell
  5. If m = 0 Then M = 2 * 10 ^ 6
  6. n = 6
  7. s = "abc2def3hjk4lm5npq7rst8uvw9xyz" ' primitive characters deliberately break numbers evenly into letters
  8. S1 = "2345789" ' Only number
  9. S2 = "abcdefhjklmnpqrstuvwxyz" ' letters only
  10. L = Len (s)
  11. L1 = Len (S1)
  12. L2 = Len (s2)
  13. ReDim arr (1 to L) as Boolean records an array of numeric positions for later checking for numbers
  14. For i = 1 to L
  15. If IsNumeric (Mid (S, I, 1)) then arr (i) = True
  16. Next
  17. ReDim brr& (1 to n-1) ' password first 5-bit corresponding ordinal base value for future calls to save compute time
  18. For i = 1 to n-1
  19. K = k + L ^ (i-1)
  20. BRR (i) = l ^ (i-1)
  21. Next
  22. ReDim crr$ (1 to K * L) ' defines an array that records the corresponding ordinal of the first 5 characters for easy elimination of duplicates
  23. t = "VN" & String (N, "") ' generates whitespace characters that require length
  24. Open Activeworkbook.path & "\password.txt" for Output as #1 ' opening txt record file
  25. Randomize ' Random seed initialization to get different random sequences each time the macro runs
  26. For i = 1 to M ' generates a password of the specified number of =m
  27. Do
  28. C1 = 0:C2 = 0:k = 0 ' record initialization c1= with digit c2= first 5 digits with letter k=
  29. For j = 1 to N-1
  30. R = Int (RND * l) + 1 ' get random characters
  31. K = k + R * BRR (j) ' Calculate cumulative serial number
  32. If arr (r) Then C1 = 1 Else c2 = 1 ' checks if the record contains numbers or letters
  33. Mid (T, J + 2, 1) = Mid (S, r, 1) ' Replace the random character with the specified position
  34. Next
  35. If C1 + c2 = 2 Then ' if the values and characters contain
  36. R = Int (RND * l) + 1 ' then you can take any value
  37. Mid (T, J + 2, 1) = Mid (S, r, 1)
  38. Else
  39. If C1 = 0 Then ' If the number is not included
  40. R = Int (Rnd * L1) + 1 ' That last join number
  41. Mid (T, J + 2, 1) = Mid (S1, R, 1)
  42. Else ' If it does not contain letters
  43. R = Int (RND * L2) + 1 ' last join letter
  44. Mid (T, J + 2, 1) = Mid (S2, r, 1)
  45. End If
  46. End If
  47. If CRR (k) = "then" if the first 5-bit sequence number is not duplicate record is empty
  48. CRR (k) = T ' records this valid result for future check repetition
  49. Print #1, T ' record to TXT file
  50. Exit Do ' exits this calculation
  51. Else ' If the sequence number already has a record
  52. If InStr (CRR (k), T) = 0 Then further checks if it is the same if different is valid
  53. CRR (k) = CRR (k) & "," & T "records add new results for future check duplicates
  54. Print #1, T ' record to TXT file
  55. Exit Do ' exits this calculation
  56. Else
  57. CNT = cnt + 1 ' statistical repetition count
  58. End If
  59. End If
  60. Loop
  61. Next
  62. Close #1 ' closing TXT file
  63. ActiveCell.Offset (, 1) = Format (Timer-tms, "0.000")
  64. ActiveCell.Offset (, 2) = cnt
  65. ActiveCell.Offset (1). Activate
  66. MsgBox Format (Timer-tms, "0.000s") & CNT & "/" & M
  67. End Sub

The "Dictionary nesting Algorithm", which generates 2 million of non-repeating passwords, breaks through the obstacles that the dictionary method is bound to crash in a large number.
But the actual speed is still relatively slow, 3-5 times slower than my array to prevent repetition.

  1. Sub Test5 ()
  2. Dim d, I&, M&, S, t$, t1$, t2$, Cnt&, tms#
  3. TMS = Timer
  4. m = activecell:if m = 0 Then M = 2 * 10 ^ 6
  5. ' s = Split ("2 3 4 5 7 8 9 A B C D E F H J K L M N P Q R s T U V W X Y Z")
  6. s = Split ("A B C 2 D E F 3 H J K 4 L M 5 N P Q 7 R S T 8 U V W 9 X Y Z")
  7. Open Activeworkbook.path & "\password.txt" for Output as #1
  8. Set d = CreateObject ("Scripting.Dictionary")
  9. Randomize
  10. Do
  11. T1 = ""
  12. For i = 1 to 2
  13. T1 = T1 & S ((RND * 30))
  14. Next
  15. If not d.exists (t1) then Set d (T1) = CreateObject ("Scripting.Dictionary")
  16. ' Only the first 2 characters are added as keys to the dictionary, and each new key is set as a nested dictionary.
  17. Do
  18. T2 = ""
  19. For i = 1 to 4
  20. T2 = T2 & S ((RND * 30))
  21. Next
  22. If not D (T1). Exists (T2) Then ' after four bit in dictionary if not repeating
  23. t = "VN" & T1 & T2 ' pin it Out
  24. If t like "*[0-9]*" and then ' confirms that it contains a number
  25. If t like "vn*[a-z]*" then ' confirm with letters
  26. D (T1) (t2) = "" "The password is effectively added to the nested dictionary to exclude future duplicates
  27. Print #1, t ' output results to TXT file
  28. CNT = cnt + 1 ' Active statistics +1
  29. Exit do
  30. End If
  31. End If
  32. End If
  33. Loop
  34. Loop Until cnt = m ' reaches target number such as 2 million after exiting
  35. Close #1 ' closing TXT file
  36. ActiveCell.Offset (, 1) = Format (Timer-tms, "0.000")
  37. ActiveCell.Offset (1). Activate
  38. MsgBox Format (Timer-tms, "0.000s") & M
  39. ' The following is a count of the number of keywords in each nested dictionary.
  40. KRR = D.keys
  41. For i = 0 to D.count-1
  42. Cells (i + 2, 5) = d (KRR (i)). Count
  43. Next
  44. Set d = Nothing
  45. End Sub

  1. Sub Test ()
  2. Dim d, I&, M&, S, t$
  3. Randomize
  4. m=200000 ' recommended that no more than 200,000 of the dictionary operation speed is significantly reduced.
  5. s = Split ("A B C 2 D E F 3 H J K 4 L M 5 N P Q 7 R S T 8 U V W 9 X Y Z") ' Press My sequence faster
  6. Set d = CreateObject ("Scripting.Dictionary")
  7. t = "VN" & String (6, "") ' Generate password template
  8. Do
  9. For i = 3 to 8
  10. Mid (t, I, 1) = S (Int (RND * 30)) ' Direct substitution faster
  11. Next
  12. If t like "*[0-9]*" then if T like "vn*[a-z]*" then D (t) = "" ' Check if the number letters are included when adding a dictionary
  13. Loop Until D.count = M ' To exit the Do loop when no duplicates in the dictionary reach m value
  14. ' [A1]. Resize (m) = Application.transpose (d.keys) ' Output results note only 65536 below can be used transpose
  15. Set d = Nothing
  16. End Sub

Excel randomly generates data 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.