Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Simple Random String Generator Numbers and Letters Only

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
Bitcoin Address: 1KQCBvevk1sA4wSj7gugv3sf5yjUygAzcv
🙂 linq https://hackforums.net/showthread.php?ti...id44524804
(Jun 14, 2015 09:36 PM)Only3 Wrote:  🙂 linq https://hackforums.net/showthread.php?ti...id44524804

Confused as to what you're getting at?
(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.
Skype: happy.cbb
0x38E6A0912657E
(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)
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.
(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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by littleblueguy24Jun 14, 2022 12:03 AM 2 120 Jun 14, 2022 12:03 AM
Last Post: littleblueguy24
[C#] by draxyeeDec 28, 2019 03:44 PM 2 294 Dec 28, 2019 03:44 PM
Last Post: Eternal Darkness
[Python] by GREGORYNov 26, 2019 07:02 AM 2 239 Nov 26, 2019 07:02 AM
Last Post: Kallu609