Hack Forums
[.NET] Simple Random String Generator Numbers and Letters Only - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Simple Random String Generator Numbers and Letters Only (/showthread.php?tid=4865342)



Simple Random String Generator Numbers and Letters Only - Vysse - Jun 14, 2015

I needed something like this and noticed nobody wrote a simple solution on here so I wrote one up myself real quick. Literally took me a minute to do no credit needed or anything just hope it helps someone.


Code
Public Shared Function RandomStringGen(ByVal numberOfChars As Integer) As String
            Dim rnd As New System.Random
            Dim chrs As Char() = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
            Dim rndString As String = Nothing
            For i = 0 To numberOfChars - 1
                rndString &= chrs(rnd.Next(chrs.Length - 1))
            Next
            Return rndString
        End Function



* - Only3 - Jun 14, 2015

🙂 linq https://hackforums.net/showthread.php?tid=4655950&pid=44524804#pid44524804


* - Vysse - Jun 15, 2015

(Jun 14, 2015 09:36 PM)Only3 Wrote:  ðŸ™‚ linq https://hackforums.net/showthread.php?tid=4655950&pid=44524804#pid44524804

Confused as to what you're getting at?


* - Hiiro - Jun 15, 2015

(Jun 15, 2015 12:12 AM)Vysse Wrote:  Confused as to what you're getting at?
In the replies of the thread he posted he's also providing a LINQ example to what you're giving.


* - DBots - Jun 15, 2015

(Jun 14, 2015 08:29 PM)Vysse Wrote:  I needed something like this and noticed nobody wrote a simple solution on here so I wrote one up myself real quick. Literally took me a minute to do no credit needed or anything just hope it helps someone.

Code
Public Shared Function RandomStringGen(ByVal numberOfChars As Integer) As String
      Dim rnd As New System.Random
      Dim chrs As Char() = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
      Dim rndString As String = Nothing
      For i = 0 To numberOfChars - 1
            rndString &= chrs(rnd.Next(chrs.Length - 1))
      Next
      Return rndString
      End Function

May have to change that string to a char array (.ToCharArray).
[Image: cffy5tb.png]

Also, I ran it back to back and look at the output:
Spoiler (Click to View)


* - uint32 - Jun 15, 2015

Fixed.
Code
Sub Main()
      Dim charset() As Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
      Dim rdn1 As New System.Random

      For i = 0 To 10
      Console.Write(charset(rdn1.Next(0, charset.Length - 1)))
      Next
      Console.ReadLine()
      Main()
    End Sub

Just edit the forloop for longer strings and convert to a function to get the value returned.


* - Vysse - Jun 16, 2015

(Jun 15, 2015 01:52 PM)DBots Wrote:  May have to change that string to a char array (.ToCharArray).
[Image: cffy5tb.png]

Also, I ran it back to back and look at the output:
Spoiler (Click to View)

That's probably because of how Random gets its random number. It has something to do with the time of your pc. If you run the random numbers within the same milisecond it will return the same results. I can fix this by adding a thread.sleep(1) but it shouldn't be needed as you are unlikely to be generating a list like this. I believe that is the issue anyways. I could be wrong. As for the Char array if you have strict options on then yes it'll require that. I do not have them enabled.