Results 1 to 13 of 13

Thread: random characters

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    118

    random characters

    Okay, so below we have a string:

    `1234567890-=~!@#$%^&*()_+qwertyuiop[]\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:"zxcvbnm,./ZXCVBNM<>?

    For each character of this string I want a new character out of the string and then remove the character from the list of characters that still maybe used for other characters. It may not get the same character, you could basically just call this encryption, but it's not what I am making. I don't want to waste my time doing this one hour while VB can do this for me in <1 second.

    So any code would be very appreciated!

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

    Re: random characters

    A hint

    Code:
        Dim prng As New Random
    
        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim allowedCH As New System.Text.StringBuilder("`1234567890-=~!@#$&#37;^&*()_+qwertyuiop[]\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:""zxcvbnm,./ZXCVBNM<>?")
            Dim newRandCharString As New System.Text.StringBuilder
    
            Stop
            'press F8 and use the debugger to gain understanding
    
            Do While allowedCH.Length > 0
                Dim idx As Integer = prng.Next(allowedCH.Length)
                newRandCharString.Append(allowedCH(idx))
                allowedCH.Remove(idx, 1)
            Loop
            Debug.WriteLine(newRandCharString.ToString)
        End Sub
    Last edited by dbasnett; Dec 12th, 2010 at 08:26 PM.
    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    118

    Re: random characters

    I am sorry to say this, but I have no idea what you are doing with the System.Text.StringBuilders.

    The only thing I understand about it is: when allowed characters > 0 then it will 'Append' a StringBuilder.


    I'd like some more information about this code, and if possible the full code. Thanks in advance!

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

    Re: random characters

    Quote Originally Posted by Teunjack View Post
    I am sorry to say this, but I have no idea what you are doing with the System.Text.StringBuilders.

    The only thing I understand about it is: when allowed characters > 0 then it will 'Append' a StringBuilder.


    I'd like some more information about this code, and if possible the full code. Thanks in advance!
    I edited my previous post so you could test the code. Here is a link to the documentation: http://msdn.microsoft.com/en-us/libr...ngbuilder.aspx
    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

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    118

    Re: random characters

    Sorry, but i'm having an error at the following line:

    vb.net Code:
    1. Dim idx As Integer = prng.Next(allowedCH.Length)

    Error: System.NullReferenceException was unhandled. Object reference not set to an instance of an object.

    I think i'll have to explain what I want better: I want to pick it a new char for the current char, and note the current char in RichTextBox1 and the new char after it, and after that add a VbCrlf/VbNewLine, so you'll get like some kind of 'encryption alphabet'.

    I would greatly appreciate this code!

  6. #6

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: random characters

    It's a random object, and the declaration is the very first line of his post.

    @Teunjack: Did you get that "Private prng as New Random" line as DBas has?

    @DBas: F8 is not going to work for all configurations of VS. For many of us, it is F10 and F11 that are used, instead.
    My usual boring signature: Nothing

  8. #8

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

    Re: random characters

    Quote Originally Posted by Shaggy Hiker View Post
    It's a random object, and the declaration is the very first line of his post.

    @Teunjack: Did you get that "Private prng as New Random" line as DBas has?

    @DBas: F8 is not going to work for all configurations of VS. For many of us, it is F10 and F11 that are used, instead.
    Did not know that shag, thanks.
    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

  10. #10
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: random characters

    Yet ANOTHER randomise question... at least this one has the decency to provide a different datatype than Integers

    I'm also sensing a request to be able to pull characters from the randomised source, rather than just randomising the whole thing... since VB doesn't have iterator blocks yet, I did a poor man's Enumerable (I can't be faffed implementing IEnumerable and IEnumerator, so this'll do)

    vbnet Code:
    1. Public Class RandomCharacters
    2.  
    3.     Private ReadOnly m_allowedCharacters() As Char
    4.     Private m_remainingCharacters As Integer
    5.     Private ReadOnly m_random As System.Random
    6.  
    7.     Public Sub New(ByVal allowedCharacters As String, ByRef random As System.Random)
    8.  
    9.         m_allowedCharacters = allowedCharacters.ToCharArray
    10.         m_remainingCharacters = m_allowedCharacters.Length
    11.         m_random = random
    12.  
    13.     End Sub
    14.  
    15.     Public ReadOnly Property CharactersRemaining As Boolean
    16.         Get
    17.             Return m_remainingCharacters > 0
    18.         End Get
    19.     End Property
    20.  
    21.     Public Function GetNextCharacter() As Char
    22.         If m_remainingCharacters < 1 Then
    23.             Throw New InvalidOperationException("Cannot retrieve more characters from source")
    24.         End If
    25.  
    26.         ' We are considering the characters in the range 0 -> # remaining characters
    27.         ' Get a random element from within that range and store a copy of it to return
    28.         ' at the end of this iteration
    29.         Dim nextCharacterIndex As Integer = m_random.Next(m_remainingCharacters)
    30.         Dim returnCharacter As Char = m_allowedCharacters(nextCharacterIndex)
    31.  
    32.         ' Place the last character from the range we are considering
    33.         ' (will not be considered next time) into the place where we
    34.         ' just got the character from.
    35.         m_allowedCharacters(nextCharacterIndex) = m_allowedCharacters(m_remainingCharacters - 1)
    36.  
    37.         ' And now decrement the range that we will consider for the
    38.         ' next iteration
    39.         m_remainingCharacters = m_remainingCharacters - 1
    40.  
    41.         Return returnCharacter
    42.  
    43.     End Function
    44. End Class

    Usage:

    vbnet Code:
    1. Dim random As New System.Random
    2. Dim allowedCharacters As String = "`1234567890-=~!@#$%^&*()_+qwertyuiop[]\QWERTYUIOP{}|asdfghjkl;'ASDFGHJKL:""zxcvbnm,./ZXCVBNM<>?"
    3. Dim allowedCharacters2 As String = "abc"
    4.  
    5. For i = 1 To 10
    6.     Dim randomCharacters As New RandomCharacters(allowedCharacters2, random)
    7.  
    8.     Do While randomCharacters.CharactersRemaining
    9.         Console.Write(randomCharacters.GetNextCharacter)
    10.     Loop
    11.     Console.WriteLine()
    12. Next

    For an explanation of how this algorithm works, see a previous post.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: random characters

    Quote Originally Posted by dbasnett View Post
    Did not know that shag, thanks.
    I learned it here. It has something to do with one of the option settings, but I forget which one. Something to make it key compatible with VB6.
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    118

    Re: random characters

    I'm sorry but I still don't understand the source.

    What I want is that I have a RichTextBox called RichTextBox1, which will get filled with this:
    ` -> j
    1 -> ;
    2 -> Z

    What I mean by that is that every character will get a new random character which isn't used yet, I hope I explained better this time..

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

    Re: random characters

    Code:
        Const allowedCH As String = "abcdefghijklmnopqrstuvwxyz1234567890"
        Dim prng As New Random
        Dim newRandCharString As New System.Text.StringBuilder
    
        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button1.Click
            'get a new random character, and remove from list
            If newRandCharString.Length = 0 Then
                Debug.WriteLine("no more characters")
                Exit Sub
            End If
            Dim idx As Integer = prng.Next(newRandCharString.Length)
            RichTextBox1.AppendText(newRandCharString(idx) & Environment.NewLine)
            newRandCharString.Remove(idx, 1)
            RichTextBox1.ScrollToCaret()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button2.Click
            'restart
            RichTextBox1.Clear()
            newRandCharString.Length = 0
            newRandCharString.Append(allowedCH)
        End Sub
    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

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