Well I have made this random string generate but I have recently noticed a fatal flaw. When I generate a random string, close the program and generate another random string it is the same as the first generation. Here is the code:
Public Function RandomString(ByVal length As Integer) As String
Dim strb As New System.Text.StringBuilder
Dim chars() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
Dim UpperBound As Integer = UBound(chars)
For x As Integer = 1 To length
strb.Append(chars(Int(Rnd() * UpperBound)))
Next
Return strb.ToString
End Function
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
Try
System.Diagnostics.Process.Start("Link Removed!")
Catch
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim repeatCnt As Integer
'Check for valid numeric entry
If Integer.TryParse(TextBox2.Text, repeatCnt) Then
For repeatIdx As Integer = 1 To repeatCnt
Dim rndstring As String
'Generate random string...
rndstring = RandomString(24)
'...and append to text box with a line break
RichTextBox1.Text &= rndstring & vbCrLf
Next
Else
MessageBox.Show("Please enter a valid integer number in the text box")
End If
End Sub
Your problem is that you’re running Rnd() instead of using the Random class.
In order to keep
Rnd()from giving the same sequence of “random” numbers every time, Random.Next() should be called (in order to use the machine clock for the initial value / seed).So in your case: