CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    [RESOLVED] Random string

    ¿there are already some function in .NET which returns a random string?

    (Is better if it contains chars which humans do not use frequently, like ╚)

    maybe there are some to generate random keys.

    this generate random paths:

    System.IO.Path.GetRandomFileName() As String
    for example, it returns:"gdhxyjvj.fnf"

    Is the best I have, but I would like less usual chars, and I are being lazy to write a random routine.

    An obscure class is System.Security.Cryptography. Looks promising, but I can't locate a random string function.
    _

  2. #2
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: Random string

    Code:
        Private Function GenerateKey(ByVal KeyLength As Integer) As String
            Dim RandomClass As New Random
            Dim Key As String = ""
    
            For i As Integer = 1 To KeyLength
                Dim iNextRnd As Integer = RandomClass.Next(0, 255)
                Key &= Chr(iNextRnd).ToString
            Next
            Return Key
        End Function
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  3. #3
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Random string

    Quote Originally Posted by HairyMonkeyMan
    Code:
        Private Function GenerateKey(ByVal KeyLength As Integer) As String
            Dim RandomClass As New Random
            Dim Key As String = ""
    
            For i As Integer = 1 To KeyLength
                Dim iNextRnd As Integer = RandomClass.Next(0, 255)
                Key &= Chr(iNextRnd).ToString
            Next
            Return Key
        End Function
    I eventually made:
    Code:
    	Public Function RandomString(ByVal Lenght As Integer) As String
    		Dim Answer As New System.Text.StringBuilder()
    		For i As Integer = 1 To Lenght   ' for character 1 to Lenght
    			Randomize()				 
    			Answer.Append(Chr(256 * Rnd()))
    		Next i
    		Return Answer.ToString
    	End Function
    And your code made me do:
    Code:
    	Public Function RandomString(ByVal Lenght As Integer) As String
    		Dim RandomClass As New Random
    		Dim Answer As New System.Text.StringBuilder()
    
    		For i As Integer = 1 To Lenght   ' for character 1 to Lenght   
    			Answer.Append(Chr(RandomClass.Next(0, 255)))
    		Next i
    		Return Answer.ToString
    	End Function
    Which I think is better.
    Thanks you.
    _

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Random string

    What exactly is the requirement?

    Do you just want a random string or are you looking for encryption?

  5. #5
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Random string

    Quote Originally Posted by Shuja Ali
    What exactly is the requirement?

    Do you just want a random string or are you looking for encryption?
    Just random string. I need it to have a low probability to be created/utilised/needed by the User.

    Is only needed to this thread: Smallest and Longest String


    I hate writing long lines of code, when performance is not a concern, and was also looking for learn new .NET resources.
    _

  6. #6
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: Random string

    just a side note:

    you should store the RandomClass reference as a module-wide variable.. each time it is instantiated, it reverts back to the first character and outputs the same sequence. If it is module wide, you only need to instantiate it once.

    Regards
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

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

Featured