|
-
Oct 29th, 2004, 12:18 PM
#1
Thread Starter
PowerPoster
random string gen prob
hi
i cant seem to get this work!
it only uses the letters up to the specified number, but i want it to use the letters that has been specified maximum, ie if the letter was z it should use all letters up to and including z
whats the problem?
Code:
int maximumLength = 9;
string minLetter = "a";
string maxLetter = "z";
string alphabet = "abcdefghijklmnopqrstuvwxyz";
string theMainString = null;
int minNum = 0;
string finalResult = null;
//MessageBox.Show(DoRandThing(9,10,'a','h'));
//return;
for (int i = 0; i < alphabet.Length; i++)
{
if (alphabet[i].ToString().Equals(minLetter))
{
for (int counter = i; counter < alphabet.Length; counter++)
{
if (!alphabet[counter].ToString().Equals(maxLetter))
{
theMainString = theMainString + alphabet[counter].ToString();
}
//if we found the last letter, break out the loop!
if(alphabet[counter].ToString().Equals(maxLetter))
{
theMainString = theMainString + alphabet[counter].ToString();
break;
}
}
}
}
int randomNumber = ran.Next(minNum, maximumLength);
if (randomNumber == 0)
{
randomNumber = 1;
}
for (int counter = 0; counter < randomNumber; counter++)
{
finalResult = finalResult + theMainString[ran.Next(minNum, maximumLength)].ToString();
}
MessageBox.Show(finalResult.ToString());
-
Oct 29th, 2004, 04:37 PM
#2
I can offer you a routine that I wrote a while ago.
Code:
public static string getNewPassword()
{
Random objRnd = new Random();
StringBuilder sbPassword = new StringBuilder();
string strAlpha = "abcdefghigklmnopqrstuvwxyz";
string strAlphaCap = "ABCDEFGHIGKLMNOPQRSTUVWXYZ";
string strNumeric = "1234567890";
string strTemp = "";
int intRndChar = 0;
try
{
for (int i=1; i<9; i++)
{
int intAlphaNumber = objRnd.Next(1, 4);
if (intAlphaNumber == 1)
strTemp = strAlpha;
else if (intAlphaNumber == 2)
strTemp = strAlphaCap;
else
strTemp = strNumeric;
intRndChar = objRnd.Next (1, strTemp.Length);
string strChar = strTemp.Substring(intRndChar, 1);
sbPassword.Append (strChar);
}
return sbPassword.ToString();
}
catch (Exception ex)
{
Console.Write ( ex.Message + " " + strTemp + " length: " + strTemp.Length + " rnd char: " + intRndChar);
return "";
}
}
-
Oct 29th, 2004, 05:24 PM
#3
Thread Starter
PowerPoster
thats great i really do appreciate it and will use it but im trying to figure out why my code doesnt work. i kinda have the full version ish working but doesnt give me a's!
-
Oct 29th, 2004, 08:15 PM
#4
Thread Starter
PowerPoster
ok i have my own version working - yippeee! BUT lol...
if we enter a max chars of 255, it only likes z's!
Code:
Random rand;
int maxLengthOfChars = 255;
int minLengthOfChars = 0;
string maxChar = "a";
string minChar = "z";
string alphabet = "abcdefghijklmnopqrstuvwxyz";
string tempString = "";
string finalString = "";
for (int counter = 0; counter < alphabet.Length; counter++)
{
if(alphabet[counter].ToString().Equals(minChar))
{
for (int counter2 = counter; counter2 < alphabet.Length; counter2++)
{
tempString += alphabet[counter2].ToString();
if (alphabet[counter2].ToString().Equals(maxChar))
{
break;
}
}
}
}
int randomStartPoint = 0;
randomStartPoint = rand.Next(minLengthOfChars, maxLengthOfChars + 1);
if (randomStartPoint == 0)
{
if (minLengthOfChars == 0)
{
randomStartPoint = 1;
}
else
{
randomStartPoint = minLengthOfChars;
}
}
int internalRandom = 0;
for (int counter = 0; counter < randomStartPoint; counter++)
{
internalRandom = rand.Next(0, tempString.Length);
if(finalString.Length != maxLengthOfChars)
{
finalString += tempString[internalRandom].ToString();
}
if(finalString.Length < minLengthOfChars)
{
finalString += tempString[internalRandom].ToString();
}
}
MessageBox.Show(finalString + " " + finalString.Length.ToString());
}
private void Form2_Load(object sender, System.EventArgs e)
{
rand = new Random();
}
-
Oct 29th, 2004, 08:20 PM
#5
Thread Starter
PowerPoster
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|