如何生成随机数字和字母串,形式(2个字母+4个数字+ 2个字母)示例AD1256Cv

时间:2017-07-14 15:13:37

标签: c# string function random

我想创建一个函数,返回一个随机的数字和字母串,形式(2个字母+4个数字+ 2个字母)示例AD1256Cv

5 个答案:

答案 0 :(得分:3)

使用此 -

private string RandomString()
        {
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            var numbers = "0123456789";
            var stringChars = new char[8];
            var random = new Random();

            for (int i = 0; i < 2; i++)
            {
                stringChars[i] = chars[random.Next(chars.Length)];
            }
            for (int i = 2; i < 6; i++)
            {
                stringChars[i] = numbers[random.Next(numbers.Length)];
            }
            for (int i = 6; i < 8; i++)
            {
                stringChars[i] = chars[random.Next(chars.Length)];
            }

            var finalString = new String(stringChars);
            return finalString;
        }

答案 1 :(得分:2)

我想我会在这里扔两分钱。基本上,我使用ASCII来选择大写字母,小写字母或数字。重要的见解是,如果数字对应于可打印的ASCII字符,则可以将整数转换为char;例如,(char)97'a'

public void PrintRandom() {
   Random r = new Random();
   // Decide how long the string will be
   int strLength = r.Next(1, 10);

   var sb = new StringBuilder();

   for (int i = 0; i < strLength; i++) {
      // Decide whether to add an uppercase letter, a lowercase letter, or a number
      int whichType = r.Next(0, 3);
      switch (whichType) {
        // Lower case letter
        case 0: sb.Append((char)(97 + r.Next(0, 26)));
          break;
        // Upper case letter
        case 1: sb.Append((char)(65 + r.Next(0, 26)));
           break;
        // Number
        case 2:
           sb.Append((char)(48 + r.Next(0, 10)));
           break;
      }
   }

   Console.WriteLine(sb.ToString());
   Console.ReadLine();
}

答案 2 :(得分:0)

一种简单的方法是使用数字和字母在字符编码中连续排列的事实(例如ASCII等)。

var rand = new Random();
var arr = new char[] {
        (char)rand.Next('A', 'Z'), 
        (char)rand.Next('A', 'Z'), 
        (char)rand.Next('0', '9'),
        (char)rand.Next('0', '9'),
        (char)rand.Next('0', '9'),
        (char)rand.Next('0', '9'),
        (char)rand.Next('A', 'Z'),
        (char)rand.Next('A', 'Z')};
var randStr = new string(arr);
Console.WriteLine(arr);

这对于混合的大写和小写字母不起作用,因为它们不是彼此相邻的连续。

答案 3 :(得分:0)

一些让你入门的线索

生成0到25之间的随机数。请参阅How do I generate a random int number in C#?

int rand = randomNumber(0, 25);

从一个字符数组中选择该元素

var randchar = "abcdefghij.....z"[rand];

你现在有一个随机字符。重复,为数字做一个类似的事情,重复等等。

答案 4 :(得分:0)

Random rndGenerator = new Random();
StringBuilder myRandomString = new StringBuilder();

for (int i = 0; i < 10; i++) // set how many random characters we want to generate
{
    bool isAlpha = Convert.ToBoolean(rndGenerator.Next(0, 2));
    int rndNumber = isAlpha ? rndGenerator.Next(0, 26) : rndGenerator.Next(0, 10); // set the boundry 26 letters in alphabet, 10 numbers
    if (isAlpha)
    {
        bool isUpper = Convert.ToBoolean(rndGenerator.Next(0, 2));
        rndNumber += isUpper ? 65 : 97; // add an offset of 65 which gets us to an A in the ASCII table, 97 is same for a
    }

    myRandomString.Append(isAlpha ? ((char)rndNumber).ToString() : rndNumber.ToString() );
}

Console.WriteLine(myRandomString.ToString());