Results 1 to 8 of 8

Thread: Random letters/numbers...

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Random letters/numbers...

    This will generate Random numbers and letters at will, i know theres another kind, i just like making my own stuff
    VB Code:
    1. Private Sub Command1_Click()
    2. Call RndLetter(True)
    3. End Sub
    4.  
    5. Private Function RndLetter(Numbers As Boolean)
    6. Dim AscChr As String, ChrAsc As String, rnduplow As Integer
    7. Randomize
    8.       rnduplow = Int((3 * rnd) + 1)
    9.        AscChr = Abs(Int(97 - 122) * rnd - 97)
    10.         ChrAsc = Chr(AscChr)
    11.             If rnduplow = 1 Then
    12.                  MsgBox ChrAsc
    13.             ElseIf rnduplow = 2 Then
    14.                  MsgBox UCase(ChrAsc)
    15.             ElseIf rnduplow = 3 Then
    16.                 MsgBox Int((9 * rnd) + 1)
    17.             End If
    18. End Function

    And heres the way everyone else does it:
    VB Code:
    1. Public Function CreateRandomLetter(ByVal pblnUpperCase As Boolean) As String
    2. Dim lngAsc  As Long
    3. Dim strChar As String
    4.     Randomize
    5.     lngAsc = vbKeyA + CLng(Rnd * (vbKeyZ - vbKeyA))
    6.     If pblnUpperCase Then
    7.         strChar = LCase$(Chr$(lngAsc))
    8.     Else
    9.         strChar = LCase$(Chr$(lngAsc))
    10.     End If
    11.     CreateRandomLetter = strChar
    12. End Function

    enjoy guys

    edit**
    and heres a way to generate random anything

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Call RndAny
    5. End Sub
    6.  
    7. Private Sub RndAny()
    8. Dim AscChr As String, ChrAsc As String
    9. Randomize
    10.        AscChr = Abs(Int(0 - 255) * Rnd)
    11.         ChrAsc = Chr(AscChr)
    12.                  MsgBox ChrAsc
    13. End Sub
    Last edited by |2eM!x; Jun 4th, 2005 at 11:42 PM.

  2. #2
    Frenzied Member Inuyasha1782's Avatar
    Join Date
    May 2005
    Location
    California, USA
    Posts
    1,035

    Re: Random letters/numbers...

    You just saved me from spending 20 minutes running a forum search! Nice job

    Nice password make too:

    VB Code:
    1. Private Sub Form_Load
    2. Dim AscChr As String, ChrAsc As String
    3. Randomize
    4.        AscChr = Abs(Int(0 - 255) * Rnd)
    5.         ChrAsc = Chr(AscChr)
    6. Msgbox "Your password is " & ChrAsc
    7. Do Until Pass = ChrAsc
    8. Pass = Inputbox ("What is your password")
    9. If Pass = ChrAsc Then
    10.      Exit Do
    11. End If
    12. Msgbox "Incorrect password, please try again"
    13. Loop
    14. End Sub

    Like it though
    Last edited by Inuyasha1782; Jun 5th, 2005 at 01:31 AM.
    Age - 15 ::: Level - Advanced
    If you find my post useful please ::Rate It::


  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Random letters/numbers...

    yup, also change the 0's to 1's, you dont want nulls in there

  4. #4
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Chesterfield, Mi
    Posts
    259

    Re: Random letters/numbers...

    Went through some of my really old code. I wrote a little generator thing much similar to these and just wondered how it compared for speed.

    I measured by ticks, 100 words at 1000 characters per word
    Write was dumping the words to a txt file.

    Using the RndLetter Ascii func was slowest:
    ASC Speed: 2141 Write: 15
    ASC Speed: 2156 Write: 31
    ASC Speed: 2203 Write: 16

    Using the CreateRandomLetter vbkey func:
    Call Speed: 1828 Write: 15
    Call Speed: 1906 Write: 32
    Call Speed: 1906 Write: 16

    Using my old code from highschool era:
    Gen Speed: 359 Write: 16
    Gen Speed: 375 Write: 15
    Gen Speed: 359 Write: 16

    Here's the code... No idea why it spanks the others so bad
    And yes I know it looks bad and is huge... but it's fast I didn't bother to pretty it up.

    I included the little bit I wrote to test the speeds as well (goes to debug window)

    VB Code:
    1. Public Function WordGen(ByVal Length As Single)
    2.         Length = Length * 2
    3.         For L = 1 To Length                    'Tell It How long a Word
    4.             NumGen = Int((36 * Rnd) + 1)        'Magic Number 1 - 36
    5.             Value = NumGen                      'Record Magic Number
    6.             Letter = ""
    7.                 Select Case Value               'Change Number to a Word
    8.                     Case Is = 1
    9.                         Letter = "A"
    10.                     Case Is = 2
    11.                         Letter = "B"
    12.                     Case Is = 3
    13.                         Letter = "C"
    14.                     Case Is = 4
    15.                         Letter = "D"
    16.                     Case Is = 5
    17.                         Letter = "E"
    18.                     Case Is = 6
    19.                         Letter = "F"
    20.                     Case Is = 7
    21.                         Letter = "G"
    22.                     Case Is = 8
    23.                         Letter = "H"
    24.                     Case Is = 9
    25.                         Letter = "I"
    26.                     Case Is = 10
    27.                         Letter = "J"
    28.                     Case Is = 11
    29.                         Letter = "K"
    30.                     Case Is = 12
    31.                         Letter = "L"
    32.                     Case Is = 13
    33.                         Letter = "M"
    34.                     Case Is = 14
    35.                         Letter = "N"
    36.                     Case Is = 15
    37.                         Letter = "O"
    38.                     Case Is = 16
    39.                         Letter = "P"
    40.                     Case Is = 17
    41.                         Letter = "Q"
    42.                     Case Is = 18
    43.                         Letter = "R"
    44.                     Case Is = 19
    45.                         Letter = "S"
    46.                     Case Is = 20
    47.                         Letter = "T"
    48.                     Case Is = 21
    49.                         Letter = "U"
    50.                     Case Is = 22
    51.                         Letter = "V"
    52.                     Case Is = 23
    53.                         Letter = "W"
    54.                     Case Is = 24
    55.                         Letter = "X"
    56.                     Case Is = 25
    57.                         Letter = "Y"
    58.                     Case Is = 26
    59.                         Letter = "Z"
    60.                     Case Is = 27
    61.                         Letter = "1"
    62.                     Case Is = 28
    63.                         Letter = "2"
    64.                     Case Is = 29
    65.                         Letter = "3"
    66.                     Case Is = 30
    67.                         Letter = "4"
    68.                     Case Is = 31
    69.                         Letter = "5"
    70.                     Case Is = 32
    71.                         Letter = "6"
    72.                     Case Is = 33
    73.                         Letter = "7"
    74.                     Case Is = 34
    75.                         Letter = "8"
    76.                     Case Is = 35
    77.                         Letter = "9"
    78.                     Case Is = 36
    79.                         Letter = "0"
    80.                     Case Is = ""
    81.                         Letter = ""
    82.                     Case Else
    83.                         Letter = ""
    84.                 End Select
    85.                 Word = Word & Letter            'Record Letter
    86.                 Letter = ""
    87.                 NumGen = ""                         'Clear Place Holder
    88.             L = L + 1                           'Record that one Letter is Done
    89.         Next L                                  'Advance to Next Letter
    90.         WordGen = Word 'Save word here
    91.         Word = ""                              'Clear Word

    I know its ugly and not very clean but it's lightning greasy chicken fast.
    Attached Files Attached Files
    Last edited by mstic; Aug 18th, 2005 at 09:18 PM.

  5. #5
    New Member
    Join Date
    Feb 2006
    Posts
    1

    Question Re: Random letters/numbers...

    Does anyone know how to gernerate a set of numbers,so if I am was trying to use list of 25 numbers and I wanted to find out every possible combination of the 25 numbers, in sets of 7. example
    1 2 3 4 5 6 7

    7 5 4
    1 2 6
    7 2 5
    1 7 4
    so on and so on,

    not sure if this is really a random issue, but I couldn't think of a better way to discibe it. any help would be appreciated. Thanks

  6. #6
    Addicted Member
    Join Date
    May 2005
    Location
    USA Washington
    Posts
    191

    Re: Random letters/numbers...

    Well, to me, this is what it looks like you are trying to do. You want to find every possible combination of the(7 or 25 numbers) numbers. A combination tells you how many possible combinations that are possible. (nCr). and nCr = n!/(n-r)!/r! where n is the total number of values to choose from and where r is the number of values you choose. In your demonstration it would be 7C3 = 7!/5!/2! This doesn't really help,, but I thought what the heck, might as well post it...

  7. #7
    Junior Member
    Join Date
    Jan 2009
    Posts
    20

    Re: Random letters/numbers...

    what do i add to make it? as in textboxes?

    oh by the way is there one just like this for vb2008?

    Last edited by Choober; Jan 15th, 2009 at 08:01 PM.

  8. #8
    New Member
    Join Date
    Jan 2009
    Posts
    1

    Post Re: Random letters/numbers...

    Code:
    Const MyString = _ 
      "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    
    Private Sub Command1_Click()
    Text1.Text = RndWord(10)
    End Sub
    
    Private Function RndWord(Optional WordLength As Integer = 8) As String
      Dim TempWord As String
      Dim loopvar As Integer
      For loopvar = 1 To WordLength
        TempWord = TempWord & Mid(MyString, Int(Rnd * 62) + 1, 1)
      Next loopvar
      RndWord = TempWord
    End Function
    
    Private Function RndChar() As String
      RndChar = Mid(MyString, Int(Rnd * 62) + 1, 1)
    End Function
    How does something like this stack up speedwise?
    (this code requires a text box and a command button)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width