|
-
June 4th, 2008, 10:33 AM
#1
[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.
_
-
June 4th, 2008, 11:57 AM
#2
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
-
June 4th, 2008, 12:51 PM
#3
Re: Random string
 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.
_
-
June 4th, 2008, 12:59 PM
#4
Re: Random string
What exactly is the requirement?
Do you just want a random string or are you looking for encryption?
-
June 4th, 2008, 01:39 PM
#5
Re: Random string
 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.
_
-
June 5th, 2008, 02:51 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|