0

このロジックを使用してランダムな文字列を生成しています。これに少なくとも1桁の数字を強制するにはどうすればよいですか?

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

より高速な解決策は、文字列のランダムな位置に乱数を挿入することだと思います:

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 に答える
1

数字が含まれているかどうかを確認するには、次のようにします。

while(!resultGenerated.Any(c => Char.IsDigit(c)))
{
    // try again until you have a digit 
}
于 2013-01-28T17:21:47.537 に答える
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
  • ランダムな文字列を生成します
  • 0 から 9 までのランダムな数字を生成する
  • 文字列のランダムなインデックスにランダムな数字を挿入します。

それが十分にランダムでない場合は、ティムの答えに固執する必要があると思います.

于 2013-01-28T17:29:46.203 に答える
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 に答える