Random Generator

andybonse

New member
Joined
Mar 28, 2014
Messages
3
Programming Experience
Beginner
Hi,

I am using the below to generate random characters in a field:

VB.NET:
Dim rand As New Random                
          Dim letter As String = ""
                For i = 0 To 4
                    letter = letter & ChrW(rand.Next(Asc("A"), Asc("Z") + 1))
                Next
                textbox1.Text = letter

How would I also get it to use both lower case and uppercase, it only gives uppercase at the minute, also how would I do numbers and letters?

Thanks!
 
You'll need to create a list that contains the characters that you want to choose from because their numeric values are not sequential. Here's how I'd do it using LINQ:
Public Class Form1

    Private rng As New Random
    Private charValues As Char()

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim zeroValue = Convert.ToInt32("0"c)
        Dim nineValue = Convert.ToInt32("9"c)
        Dim capitalAValue = Convert.ToInt32("A"c)
        Dim capitalZValue = Convert.ToInt32("Z"c)
        Dim smallAValue = Convert.ToInt32("a"c)
        Dim smallZValue = Convert.ToInt32("z"c)

        Me.charValues = Enumerable.Range(zeroValue, nineValue - zeroValue + 1).Concat(Enumerable.Range(capitalAValue, capitalZValue - capitalAValue + 1)).
                                                                               Concat(Enumerable.Range(smallAValue, smallZValue - smallAValue + 1)).
                                                                               Select(Function(n) Convert.ToChar(n)).
                                                                               ToArray()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.TextBox1.Text = String.Concat(Enumerable.Range(1, 5).Select(Function(n) Me.charValues(Me.rng.Next(0, Me.charValues.Length))))
    End Sub

End Class
You can do it without LINQ if you want but the crux of the matter is that you need to select the random characters from a list that contains all the characters that you want to select from. Even if you simply write it out manually, e.g.
Private charValues As Char() = "0123456789".ToCharArray()
 
I tested this code and it works to select random numbers and both upper and lowercase letters:

VB.NET:
            Dim randnum As New Random()
            Dim num, ascnum As Integer, ch As Char
            num = randnum.Next(48, 109)
            ascnum = num
            If num > 82 Then
                ascnum = num + 14
            ElseIf num > 57 Then
                ascnum = num + 8
            End If
            ch = Chr(ascnum)
 
Guids not every Ones flavor, expecting moaned at.

VB.NET:
Public Class Form1

    Dim rng As New Random
    Dim chars As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"""

    Private Sub Foo()
        Dim value As String = New String((chars.OrderBy(Function(c) Guid.NewGuid).Take(5)).ToArray())
        MessageBox.Show(value)
    End Sub

End Class
 
Back
Top