我需要生成十位数的字母数字随机数 [英] I need to generate ten digit alphanumeric random numbers

查看:97
本文介绍了我需要生成十位数的字母数字随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成十位数的字母数字随机数



I need to generate ten digit alphanumeric random numbers

Random rnd = new Random();
string rdnumber = "" + rnd.Next(1000, 9000);

推荐答案

首先,imho,在类根级别定义随机对象的实例是个好习惯:



私有静态随机rndFunc = new Random(DateTime.Now.Millisecond);



如果我们假设通过字母数字字符表示低位和高位字母字符和数字:
First, imho, it's good practice to define an instance of the Random Object at the Class root-level:

private static Random rndFunc = new Random(DateTime.Now.Millisecond);

If we assume that by "alphanumeric characters" you mean lower, and upper, case alphabet characters, and digits:
private StringBuilder MakeAlphaNumeric(StringBuilder sb, int numChars)
{
    char ch;
    numChars++;

    while (sb.Length < numChars)
    {
        ch = Convert.ToChar(rndFunc.Next(48, 123));

        if (char.IsLetterOrDigit(ch))
        {
            sb.Append(ch);
        }
    }

    return sb;
}

测试:

private void testMakeAlpha(int howManyStrings, int howManyCharsInString)
{
    string nchars;
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < howManyStrings; i++)
    {
        nchars = MakeAlphaNumeric(sb, howManyCharsInString).ToString();

        sb.Clear();

        Console.WriteLine(nchars);
    }
}

// call: testMakeAlpha(10,10);

// output of test
v0yINKWFYuS
E473dN68cXc
yQHnLcmC0hD
Z19JJ7Q4u2v
PCXYsFkASM5
yXeYQa1r6nj
MwiIQf4NcbN
9CKqVkvmX0M
sZ8a89xJVoI
aFpGOFMa1qG

在这里使用StringBuilder有一些节约,这可能不是必需的。

There's some "economizing" on the use of 'StringBuilder going on here that's probably not necessary.


简单:



simple:

const string _availChars = "abcdefghijklmnopqrstuvwxyz0123456789";

string GenAlphaNum(int length){

      var random = new Random(); //requires seed
    return new string(Enumerable.Repeat(_availChars , length)
      .Select(s => s[random.Next(s.Length)]).ToArray());
}





您可以想到将Caps添加到可用字符列表中,但这有缺点使'A'像'a'一样可能与任何其他单个数字一样。解决这个问题的一种方法是将数字串添加两次以达到赔率,或者在字符串中找到一些聪明的上层随机字符方式。



有负载这些例子。这与我使用的javascript版本非常类似,但我没有在C#中测试过这个版本。





更新:

如果你不知道如何添加种子 - 这里有几个例子



You can toy with the idea of having Caps added to the list of available chars but that has the drawback of making 'A' as likely as 'a' as likely as any other single number. One way around this it to add the number string twice to even up the odds, or find some clever way of upper-casing random chars in the string.

There's loads of examples out these. This is pretty similar to a javascript version I use but I haven't tested this in C#


UPDATE:
In case you are not aware of how to add a seed - here are a couple of examples

const string _availChars = "abcdefghijklmnopqrstuvwxyz0123456789";

        string GenAlphaNum(int length)
        {
            //millisecond seed
            var random = new Random(DateTime.Now.Millisecond); //requires seed
            return new string(Enumerable.Repeat(_availChars, length)
              .Select(s => s[random.Next(s.Length)]).ToArray());
        }



随机随机


with constant random

const string _availChars = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random _random = null;
        Random Rand
        {
            get
            {
                if (_random == null)
                    _random = new Random(DateTime.Now.Millisecond);
                return _random;
            }
        }

        string GenAlphaNum(int length)
        {
            return new string(Enumerable.Repeat(_availChars, length)
              .Select(s => s[Rand.Next(s.Length)]).ToArray());
        }


这篇关于我需要生成十位数的字母数字随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆