Generate random characters in Golang, Python: Letters, numbers, capitalization, etc.

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The go language needs to write various function methods to achieve it. The code below represents the result of the individual writing the code in a very small amount of time and does not represent the optimal algorithm.

Func Main () {FMT. Println (' 0 ') fmt. Println (Randupstring (Ten)) fmt. Println (Randlowstring (Ten)) fmt. Println (Randintstring (5)) fmt. Println (RANDSTR (10))}//get random characters containing numbers, capitalization, etc., can be arbitrarily expanded. Func randstr (l int) string {var inibyte []byte var result bytes. Buffer for I: = 48; I < 123;  i++ {switch {case I < 58:inibyte = Append (Inibyte, Byte (i)) case I >= && i < 91:inibyte = Append (Inibyte, Byte (i)) case I >= && i < 123:inibyte = Append (Iniby TE, Byte (i)}}} var temp byte for I: = 0; I < L; {if Inibyte[randint (0, Len (inibyte))]! = Temp {temp = inibyte[randint (0, Len (inibyte))] result. WriteByte (temp) i++}} return result. String ()}//Gets the uppercase random letter Func randupstring (l int) string {var result bytes. Buffer var temp byte for I: = 0; I < L; {if randint (+) = Temp {temp = Randint (+) result.   WriteByte (temp) i++   }} return result. String ()}//Gets the lowercase random letter func randlowstring (l int) string {return strings. ToLower (Randupstring (L))}//Gets the number random number func randintstring (l int) string {var result bytes. Buffer var temp byte for I: = 0; I < L; {if Randint ()! = Temp {temp = Randint (.) WriteByte (temp) i++}} return result. String ()}func randint (min int, max int) byte {rand. Seed (time. Now (). Unixnano ()) return byte (min + rand). INTN (max-min))}

In Python, you can call the library file's own method, the key point is that the python generated characters are completely non-repeating, and the above go code to generate characters can only be adjacent to the non-repetition.

Import String,randomprint (". Join" (Random.sample (String.ascii_letters+string.digits))) #随机字符print (". Join ( Random.sample (String.ascii_letters)). Lower ()) #小写字母print (". Join (Random.sample (String.ascii_letters, 10)). Upper ()) #大写字母print (". Join (Random.sample (string.digits))) #数字
Related Article

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.