0

我正在使用这个逻辑来生成随机字符串。我如何强制它至少包含一个数字?

var charsALL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
var randomIns = new Random();
var resultGenerated = new string(
        Enumerable.Repeat(charsALL, 6)
                  .Select(stringgen => stringgen[randomIns.Next(stringgen.Length)])
                  .ToArray());
return resultGenerated.ToString();
4

7 回答 7

4
var charsALL = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
var randomIns = new Random();
int N = 6;
var rndChars = Enumerable.Range(0, N)
                .Select(_ => charsALL[randomIns.Next(charsALL.Length)])
                .ToArray();
rndChars[randomIns.Next(rndChars.Length)] = "0123456789"[randomIns.Next(10)];

var randomstr = new String(rndChars);
于 2013-01-28T17:33:31.247 回答
1

您可以检查它是否包含一个数字:

while(!resultGenerated.Any(c => Char.IsDigit(c)))
{
    // try again until you have a digit 
}
于 2013-01-28T17:21:47.537 回答
1

我认为更快的解决方案是在字符串的随机位置插入一个随机数:

char[] chars = Enumerable.Repeat( charsALL, 6 )
               .Select( stringgen => stringgen[ randomIns.Next( stringgen.Length ) ] )
               .ToArray( );

if (!chars.Any(c => Char.IsDigit(c)))
   chars[ randomIns.Next( chars.Length ) ] = randomIns.Next( 10 ).ToString( )[ 0 ];

return new string(chars);
于 2013-01-28T17:36:18.333 回答
1

您可以使用 az、AZ 和 0 -9 的 asci 代码进行随机化

        public static string GetRandomString(int randomStrLength)
        {
            Random rand = new Random(DateTime.Now.Millisecond);
            char[] randomString = new char[randomStrLength];

            int randCode = 0;
            bool containsDigit = false;

            while (containsDigit == false)
            {
                for (int i = 0; i < randomStrLength; i++)
                {
                    // Get random asci codes (allowed: a-z, A-Z, 0-9)
                    do
                    {
                        randCode = rand.Next(48, 122);
                    }
                    while (randCode > 57 && randCode < 65 || randCode > 90 && randCode < 97);

                    randomString[i] = (char)randCode;

                    // We want at least one digit.
                    if (randCode >= 48 && randCode <= 57)
                    {
                        containsDigit = true;
                    }
                }
            }
            return new String(randomString);
        }
于 2013-01-28T17:46:35.323 回答
0
  • 生成你的随机字符串
  • 生成 0 到 9 之间的随机数字
  • 在字符串的随机索引处插入随机数字。

如果这对您来说还不够随机,那么恐怕您将不得不坚持 Tim 的回答。

于 2013-01-28T17:29:46.203 回答
0

我的版本(经过试验和测试);)

namespace PasswordGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Your new password is : " + CreateRandomPassword(8));
            Console.ReadLine();
        }
        private static string CreateRandomPassword(int passwordLength)
        {
            string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";
            char[] chars = new char[passwordLength];
            Random random = new Random();

            bool containsNum = false;
            do {
                for (int i = 0; i < passwordLength; i++)
                {
                    chars[i] = allowedChars[random.Next(0, allowedChars.Length)];
                    if(Char.IsDigit(chars[i])){
                        containsNum = true;
                    }
                }
            } while(!containsNum);

            return new string(chars);
        }
    }
}
于 2013-01-28T17:51:08.837 回答
0

我不知道这是否有帮助,但这是我用来生成随机字符串的类。在生成字符串之前使用它时,有复选框来决定它是否必须至少有 1 个大写字母/符号/数字等。对某些人来说可能很冗长,但它对我有用。

 class RandomString
{
    private bool m_HasCaps;
    private bool m_HasNumbers;
    private bool m_HasSymbols;
    private int m_StringLength;
    private string characterString;
    private string randomString;

    //Set possible characters to char array rather than a string for potential future method involving chars
    private char[] lower = "abcdefghijklmnopqrstuvwxyz".ToCharArray();
    private char[] upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
    private char[] numbers = "0123456789".ToCharArray();
    private char[] symbols = "!£$%&()<>[]?#-+=".ToCharArray();


    /// <summary>
    /// The returned string can contain just lowercase or a mixture of upper, digits and symbols depending on params and
    /// the lenght of the string return can also be set
    /// </summary>
    /// <param name="caps">Does the string need to conatain at least 1 upper case</param>
    /// <param name="nums">does the string need to contain at least 1 numeral</param>
    /// <param name="symbols">does the string need to contain at least 1 symbol</param>
    /// <param name="length">the length of the string to return </param>
    public RandomString(bool caps, bool nums,bool symbols,int length)
    {
        m_HasCaps = caps;
        m_HasNumbers = nums;
        m_HasSymbols = symbols;
        m_StringLength = length;
    }

    public string CreatePassword()
    {
        BuildCharacterstring();
        BuildRandomString();

        return randomString;

    }

    //Check to see what the string must contain and add those characters to the 
    //Character string 
    private void BuildCharacterstring()
    {
        characterString = new string(lower);

        if (m_HasCaps)
        {
            string upperString = new string(upper);
            characterString += upperString;
        }

        if (m_HasNumbers)
        {
            string numberString = new string(numbers);
            characterString += numberString;
        }

        if (m_HasSymbols)
        {
            string symbolString = new string(symbols);
            characterString += symbolString;
        }
    }

    //
    private void BuildRandomString()
    {
        Random rdm = new Random();
        bool validPassword = false;

        while (validPassword == false)
        {
            //blank the string before each run otherwise you end up with double the characters
            randomString = "";

            for (int x = 0; x < m_StringLength; x++)
            {
                //select a char from the password character string
                int place = rdm.Next(0, characterString.Length);
                char character = characterString[place];
                randomString += character;

            }

            //Check that the password has the appropriate characters
            validPassword = CheckParams();
        }

    }

    //checks that each required preference is met and that the string includes at least 1 of what 
    //is selected. If the parameter is not required ie symbols are not required set it to true to pass
    //the test anyway.
    private bool CheckParams()
    {
        bool capsPassed;
        bool numeralPassed;
        bool symbolPassed;
        bool lowerPassed;

        //If caps are not required set to true to pass the test automatically
        if(m_HasCaps == false)
        {
            capsPassed = true;
        }
        else
        {
            //check if the password contains an uppercase letter
            capsPassed = (randomString.Any(char.IsUpper)) ? true : false;
        }

        if(m_HasNumbers == false)
        {
            numeralPassed = true;
        }
        else
        {
            numeralPassed = (randomString.Any(char.IsDigit)) ? true : false;
        }

        if(m_HasSymbols == false)
        {
            symbolPassed = true;
        }
        else
        {
            symbolPassed = (randomString.Any(char.IsSymbol)) ? true : false;
        }

        lowerPassed = (randomString.Any(char.IsLower)) ? true : false;

        //All 4 must pass to return true and stop password generation
        if (capsPassed && numeralPassed && symbolPassed &&lowerPassed)
        {
            return true;
        }
        else
        {
            return false;
        }
    }       
}
于 2016-07-13T12:43:43.193 回答