Generate a random RNGCryptoServiceProvider number and a random gb2312 level-1 Chinese (based on changes made on the Internet)

Source: Internet
Author: User

The random number uses the msdn example. However, the random Chinese is modified according to the gb2312 standard because the method found on the internet is repeated.

Random Number:

/// <Summary> /// generate a random number less than the absolute value of the input value // </summary> /// <param name = "NumSides"> </param> // /<returns> </returns> public static int Next (this int numSeeds) {numSeeds = Math. abs (numSeeds); if (numSeeds <= 1) {return 0;} int length = 4; if (numSeeds <= byte. maxValue) {length = 1;} else if (numSeeds <= short. maxValue) {length = 2;} return Next (numSeeds, length);} private static int Next (int numSeeds, int length) {// Create a byte array to hold the random value. byte [] buffer = new byte [length]; // Create a new instance of the RNGCryptoServiceProvider. system. security. cryptography. RNGCryptoServiceProvider Gen = new System. security. cryptography. RNGCryptoServiceProvider (); // Fill the array with a random value. gen. getBytes (buffer); // Convert the byte to an uint value to make the modulus operation easier. uint randomResult = 0x0; // here uint is used as the random number generated for (int I = 0; I <length; I ++) {randomResult | = (uint) buffer [I] <(length-1-I) * 8);} // Return the random number mod the number/of sides. the possible values are zero-based return (int) (randomResult % numSeeds );}

 

Random Chinese:

/** Gb2312 80 ** 01-09 is a special symbol. * Areas 16-55 are top-level Chinese characters, sorted by pinyin. * Area 56-87 contains second-level Chinese characters, which are sorted by the beginning or strokes. * Areas 10-15 and 8-94 are not encoded. ** All numbers start from 1 **. Each Chinese Character and symbol is expressed in two bytes. * The first byte is called "high byte" (also known as "zone byte"), and the second byte is called "low byte" (also known as "bit byte "). Low byte * "high byte" uses 0xA1-0xF7 (add the area code of area 01-87 with 0xA0), * "low byte" uses 0xA1-0xFE (add 01-94 with 0xA0 ). * Since the first-level Chinese character starts from the 16-area, the range of "high byte" in the Chinese character area is 0xB0-0xF7, and * the range of "low Byte" is 0xA1-0xFE, * The occupied code bit is 72*94 = 6768. * Five of the available spaces are D7FA-D7FE (Zone 55 90-94) */public static string GetRandomPopularSimplifiedChinese (this int length) {if (length <= 0) {return string. empty;} byte minUpper = 16; byte maxUpper = 55; byte rangeLow = 94; int addParamer = 0xA0; StringBuilder tempStr = new StringBuilder (); int tempUpper = maxUpper-minUpper + 1; encoding gb = Encoding. getEncoding ("gb2312"); for (int I = 0; I <length; I ++) {int rdUpperValue = Next (tempUpper) + minUpper; int rdLowValue; do {rdLowValue = Next (rangeLow) + 1; // The index starts from 1, so 94 seed generated Random Number + 1} while (rdUpperValue = maxUpper & rdLowValue> = 90); // The D7FA-D7FE is vacant (55-94) rdUpperValue + = addParamer; rdLowValue + = addParamer; byte [] byteArray = new byte [] {(byte) rdUpperValue, (byte) rdLowValue}; tempStr. append (gb. getString (byteArray);} return tempStr. toString ();}

 

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.