Vb. NET generate random string or random number method Summary _ Practical skills

Source: Internet
Author: User
Tags constant datetime lowercase rand

In this paper, 5 kinds of generating random numbers are introduced, which are as follows:

The first: The method of reprint

Public Enum StringType allstring = 1 ' uppercase and lowercase allnumic = 2 ' Digital str_num = 3 ' uppercase and lowercase + number Str_upper = 4 ' Capital Letter Str_lower = 5 ' Capital Letter End Enum Function generaterandom (ByVal Length as Integer, ByVal s as StringType) A s string Dim strtemp As String = "" Dim constant () As String = Nothing Select case s case stringtype.all Numic strtemp = "0,1,2,3,4,5,6,7,8,9" constant = strtemp. Split (",") case stringtype.allstring strtemp = "A,b,c,d,e,f,g,h,i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z,a,b,c,d,e, F,d,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,x,y,z "constant = strtemp. Split (",") case stringtype.str_lower strtemp = "A,b,c,d,e,f,g,h,i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z" C Onstant = strtemp. Split (",") case stringtype.str_num strtemp = "A,b,c,d,e,f,g,h,i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z,a,b,c,d,e,f, d,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,x,y,z,0,1,2,3,4,5,6,7,8,9 "constant = strtemp.
    Split (",")  Case Stringtype.str_upper strtemp = "A,b,c,e,f,d,g,h,i,j,k,l,m,n,p,q,r,s,t,u,v,w,x,y,z" constant = Strte Mp. Split (",") End Select Dim newrandom As System.Text.StringBuilder = New System.Text.StringBuilder (Length) Dim R D as Random = New Random () Dim I as Integer for i = 0 to Length-1 step i + 1 Newrandom.append constant (Rd. Next (constant.

 Length-1))) Next return newrandom.tostring () end Function

The second type: Online search

The public Function getoauthnonce () As String ' gets the random value Dim result as
    string = System.Guid.NewGuid (). ToString () result
    = result. Replace ("-", "") return to result
    . Substring (0) End
  Function
Public Function Randcode (ByVal N as Integer) as String
    Dim Arrchar as Char () = New Char () {"A" C, "B" C, "D" C, "C" C, "E"  C, "F" C, _
                      "G" C, "H" C, "I" C, "J" C, "K" C, "L" C, _
                      "M" C, "n" C, "P" C, "R" C, "Q" C, "S" C, _
                      "T" C, "U" C, "V" C, "W" C, "Z" C, "Y" C, _
                      "x" C, "0" C, "1" C, "2" C, "3" C, "4" C, _
                      "5" C, "6" C, "7" C, "8" C, "9" C, "A" C, _
                      "B" C, "C" C, "D" C, " E "C", "F" C, "G" C, _
                      "H" C, "I" C, "J" C, "K" C "," L "C," M "C, _
                      " N "C," Q "C," P "C," R "C," T "C," S "C," C ","
                      W " "C," X "C," Y "C," Z "C}
    Dim num As New StringBuilder ()
    Dim rnd As New Random (DateTime.Now.Millisecond) for
    I As Integer = 0 to n-1
      Num. Append (Arrchar (Rnd.[next] (0, arrchar.length)). ToString ())
    Next return
    Num. ToString () End
  Function

The third: the original C # conversion

Public Function Randcode (ByVal N as Integer) as String
    Dim Arrchar as Char () = New Char () {"A" C, "B" C, "D" C, "C" C, "E"  C, "F" C, _
                      "G" C, "H" C, "I" C, "J" C, "K" C, "L" C, _
                      "M" C, "n" C, "P" C, "R" C, "Q" C, "S" C, _
                      "T" C, "U" C, "V" C, "W" C, "Z" C, "Y" C, _
                      "x" C, "0" C, "1" C, "2" C, "3" C, "4" C, _
                      "5" C, "6" C, "7" C, "8" C, "9" C, "A" C, _
                      "B" C, "C" C, "D" C, " E "C", "F" C, "G" C, _
                      "H" C, "I" C, "J" C, "K" C "," L "C," M "C, _
                      " N "C," Q "C," P "C," R "C," T "C," S "C," C ","
                      W " "C," X "C," Y "C," Z "C}
    Dim num As New StringBuilder ()
    Dim rnd As New Random (DateTime.Now.Millisecond) for
    I As Integer = 0 to n-1
      Num. Append (Arrchar (Rnd.[next] (0, arrchar.length)). ToString ())
    Next return
    Num. ToString () End
  Function

Fourth: Use of VB6 conversion

Function Makerand (ByVal maxlen as Integer) As String ' when generating signatures with random string
    Dim strnewpass As String = vbNullString
    Dim lower As Long
    Dim whatsnext As Long Dim
    Upper As Long
    Dim intcounter As Long
    Randomize () for
    intcounter = 1 To maxlen
      whatsnext = Int (1-0 + 1) * RND () + 0)
      If whatsnext = 0 Then Upper
        = 122 lower
        =
      El Se
        upper =
        lower = ' End
      If
      strnewpass = strnewpass & Chr (Int (upper-lower + 1) * RND () + Lo wer))
    Next
    makerand = Strnewpass End
  Function

Fifth: Direct use of the vb.net function

  Dim Rand as Random = New system.random (10) ' Here 10 represents 10 for
   Debug.Print (Rand. Next (). ToString)

Original link: http://blog.csdn.net/lcp58006478/article/details/8958460

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.