Results 1 to 5 of 5

Thread: [RESOLVED] Create Random Strings - Issue

  1. #1

    Thread Starter
    Lively Member kshadow22's Avatar
    Join Date
    Dec 2014
    Location
    Kentucky
    Posts
    95

    Resolved [RESOLVED] Create Random Strings - Issue

    Hi,
    I am currently working on a project when button1 is pressed, it will generate different and random keys into textboxes.
    So for example, button1 is pressed and this happens to four textboxes...
    TextBox1: 1kjnkj3n
    TextBox2: 4kj32n4
    TextBox3: 5kjnssd
    TextBox4: dskjnwS
    ...This is my goal.

    BUT
    I am using this code below as a function to generate the keys, however, it does not generate a new code for every textbox, instead every textbox is assigned the same random key.
    Code:
    Private Function CipherKeyGenerated()
    
      Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi123"
            Dim r As New Random
            Dim sb As New StringBuilder
            For i As Integer = 1 To 7
                Dim idx As Integer = r.Next(0, 18)
                sb.Append(s.Substring(idx, 1))
            Next
    Return (sb.ToString())
        End Function
    To call the function for each of the textboxes under the button1_click event, I am using:
    Code:
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    TextBox1.Text = CipherKeyGenerated()
            TextBox2.Text = CipherKeyGenerated()
            TextBox3.Text = CipherKeyGenerated()
            TextBox4.Text = CipherKeyGenerated()
    End Sub

    Here is a picture of what I need the output to look like...
    Name:  Goal.PNG
Views: 388
Size:  1.7 KB

    Here is a picture of my current output and what my code does...
    Name:  CurrentOutput.PNG
Views: 338
Size:  1.2 KB

    Any suggestions, ideas, tips would be very appreciated and thank you for your time!!!

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,447

    Re: Create Random Strings - Issue

    Try movinf the dim r as New Random to outside of the function. Every time you are creating a new instance it is re-seeding the random number generator, if you create them quickly enough they will all have the same seed.

  3. #3

    Thread Starter
    Lively Member kshadow22's Avatar
    Join Date
    Dec 2014
    Location
    Kentucky
    Posts
    95

    Re: Create Random Strings - Issue

    Quote Originally Posted by PlausiblyDamp View Post
    Try movinf the dim r as New Random to outside of the function. Every time you are creating a new instance it is re-seeding the random number generator, if you create them quickly enough they will all have the same seed.
    Awesome!!! This worked perfectly!!!

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,748

    Re: [RESOLVED] Create Random Strings - Issue

    Rewrite of the function
    Code:
        Private PRNG As New Random
        Private Function CipherKeyGenerated() As String
            Static s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi123"
            Dim sb As New System.Text.StringBuilder
            For i As Integer = 1 To 7
                Dim idx As Integer = PRNG.Next(0, s.Length)
                sb.Append(s(idx))
            Next
            Return (sb.ToString())
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: [RESOLVED] Create Random Strings - Issue

    Sitten wrote a good example of Random using a number of techniques.

    http://www.vbforums.com/showthread.p...=1#post3914156

Tags for this Thread

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