我正在使用此逻辑生成随机字符串。如何强制它至少包含一个数字?
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();
答案 0 :(得分: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);
答案 1 :(得分:1)
您可以检查它是否包含数字:
while(!resultGenerated.Any(c => Char.IsDigit(c)))
{
// try again until you have a digit
}
答案 2 :(得分: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);
答案 3 :(得分:1)
您可以使用a-z,A-Z和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);
}
答案 4 :(得分:0)
如果这对你来说不够随便,我恐怕你不得不坚持Tim的回答。
答案 5 :(得分: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);
}
}
}
答案 6 :(得分: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;
}
}
}