WHAT'S NEW?
Loading...

Generate random number and string key in VB.Net


A random number is a number chosen as if by chance from various specified allocation. Computer-generated random numbers occasionally called pseudorandom numbers. Obviously, very difficult for humans to produce a string of random digits like computer. Computer can produce an output unpredictable because they given a task what they are going to work. More often, computer can generate with the combination of numbers and characters both lower and uppercase. In these tutorials, we are going to randomize both number and characters. So, here is the code.
Public Function generatecode() As Object
        Dim intrnd As Object
        Dim intstep As Object
        Dim name As Object
        Dim numlength As Object
        Dim intlenght As Object
        Dim strinput As Object
        Dim maxnum As Integer = txtmaxnum.Text
        strinput = "123456789abcdefghigklmnopqrstuvwxwzABCDEFGHIGKLHMOPQRSTUVWXYZ"
        intlenght = Len(strinput)
        numlength = maxnum
        Randomize()
        name = ""
        For intstep = 1 To numlength
            intrnd = Int(intlenght * Rnd()) + 1
            name = name & Mid(strinput, intrnd, 1)
        Next
        Return name
    End Function
We created method name generatecode so that every time the code is needed, just simply call the method.


Note: if you have knowledge about regular expression, instead typing a lot of numbers and letters just simply follow the rule of regular expression. More about regular expression.

0 comments:

Post a Comment